add custom footer
This commit is contained in:
+41
-13
@@ -30,9 +30,12 @@ var Data = FullData{
|
|||||||
// Maps are created in init()
|
// Maps are created in init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// the Header of feeds.gmi.
|
// the Header and Footer of feeds.gmi.
|
||||||
// set in Init, either to the contents of header.gmi or to the default string
|
// set in Init, either to the contents of header.gmi or to the default string
|
||||||
var Header string
|
var (
|
||||||
|
Header string
|
||||||
|
Footer string
|
||||||
|
)
|
||||||
|
|
||||||
// Timeout for requests
|
// Timeout for requests
|
||||||
var Timeout int
|
var Timeout int
|
||||||
@@ -74,30 +77,55 @@ func Init(dataPath string, localizer *localizations.Localizer) error {
|
|||||||
Data.Pages = make(map[string]*Page)
|
Data.Pages = make(map[string]*Page)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize header.gmi
|
// initialize header.gmi and footer.gmi
|
||||||
f, err = os.Open(filepath.Join(dataPath, "header.gmi"))
|
Header, err = getHeaderFooter(
|
||||||
|
dataPath, "header.gmi",
|
||||||
|
TranslateString("# %s\n\n", "files.feed_default_header", nil), "",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
footer, err := getHeaderFooter(
|
||||||
|
dataPath, "footer.gmi", "---\n\n",
|
||||||
|
TranslateString(
|
||||||
|
"=> https://git.nytpu.com/comitium/about/ %s\n",
|
||||||
|
"files.comitium_footer",
|
||||||
|
&localizations.Replacements{"version": Version, "commit": Commit},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
Footer = fmt.Sprintf("\n\n%s", footer)
|
||||||
|
|
||||||
|
Timeout = 10
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// gets a header or footer from a datapath, or uses a default. extraString is a
|
||||||
|
// string to append no matter what
|
||||||
|
func getHeaderFooter(dataPath, filename, defaultString, extraString string) (main string, err error) {
|
||||||
|
f, err := os.Open(filepath.Join(dataPath, filename))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fi, err := f.Stat()
|
fi, err := f.Stat()
|
||||||
if err == nil && fi.Size() > 0 {
|
if err == nil && fi.Size() > 0 {
|
||||||
headerBytes, err := ioutil.ReadAll(f)
|
headerBytes, err := ioutil.ReadAll(f)
|
||||||
f.Close()
|
f.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(localizer.Get("errors.read_file_error", &localizations.Replacements{"file": "header.gmi", "error": err}))
|
return "", fmt.Errorf(Localizer.Get("errors.read_file_error", &localizations.Replacements{"file": "header.gmi", "error": err}))
|
||||||
}
|
}
|
||||||
Header = string(headerBytes)
|
main = string(headerBytes)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
} else if !os.IsNotExist(err) {
|
} else if !os.IsNotExist(err) {
|
||||||
return fmt.Errorf(localizer.Get("errors.read_file_error", &localizations.Replacements{"file": "header.gmi", "error": err}))
|
return "", fmt.Errorf(Localizer.Get("errors.read_file_error", &localizations.Replacements{"file": "header.gmi", "error": err}))
|
||||||
}
|
}
|
||||||
|
|
||||||
if Header == "" {
|
if main == "" {
|
||||||
Header = TranslateString("# %s\n\n", "files.feed_default_header", nil)
|
main = defaultString
|
||||||
}
|
}
|
||||||
|
return main + extraString, nil
|
||||||
Timeout = 10
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteJSON writes a FullData to disk
|
// WriteJSON writes a FullData to disk
|
||||||
|
|||||||
+2
-7
@@ -79,11 +79,6 @@ func genDay() string {
|
|||||||
return TranslateString("%s\n", "files.generated_on", &localizations.Replacements{"date": time.Now().Format("2006-01-02 at 15:04 MST")})
|
return TranslateString("%s\n", "files.generated_on", &localizations.Replacements{"date": time.Now().Format("2006-01-02 at 15:04 MST")})
|
||||||
}
|
}
|
||||||
|
|
||||||
// agplCompliance returns a string containing a footer linking to the source code
|
|
||||||
func agplCompilance() string {
|
|
||||||
return TranslateString("\n\n---\n\n=> https://git.nytpu.com/comitium/about/ %s\n", "files.comitium_footer", &localizations.Replacements{"version": Version, "commit": Commit})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *FullData) WriteSubs(dataPath string) error {
|
func (d *FullData) WriteSubs(dataPath string) error {
|
||||||
path := filepath.Join(dataPath, "subscriptions.gmi.new")
|
path := filepath.Join(dataPath, "subscriptions.gmi.new")
|
||||||
|
|
||||||
@@ -115,7 +110,7 @@ func (d *FullData) WriteSubs(dataPath string) error {
|
|||||||
}
|
}
|
||||||
d.PagesMu.RUnlock()
|
d.PagesMu.RUnlock()
|
||||||
|
|
||||||
rawPage += agplCompilance()
|
rawPage += Footer
|
||||||
|
|
||||||
err := ioutil.WriteFile(path, []byte(rawPage), 0666)
|
err := ioutil.WriteFile(path, []byte(rawPage), 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -156,7 +151,7 @@ func (d *FullData) WriteFeed(dataPath string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rawPage += agplCompilance()
|
rawPage += Footer
|
||||||
err := ioutil.WriteFile(path, []byte(rawPage), 0666)
|
err := ioutil.WriteFile(path, []byte(rawPage), 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user