Changeset 79 in code


Ignore:
Timestamp:
Dec 11, 2023, 5:23:49 PM (18 months ago)
Author:
Izuru Yakumo
Message:

Separate the build<FORMAT> functions into their own source files

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

Location:
trunk
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/cmd/aya/main.go

    r78 r79  
    1 // $TheSupernovaDuo: marisa.chaotic.ninja/aya/cmd/aya, v0.6.9 2023-12-09 23:30:07+0000, yakumo_izuru Exp $
     1// $TheSupernovaDuo: marisa.chaotic.ninja/aya/cmd/aya, v0.7.0 2023-12-11 17:22:51+0000, yakumo_izuru Exp $
    22package main
    33
     
    1010        "path/filepath"
    1111        "strings"
    12         "text/template"
    1312        "time"
    1413
    15         "github.com/eknkc/amber"
    16         "github.com/russross/blackfriday/v2"
    17         "github.com/yosssi/gcss"
    1814        "gopkg.in/yaml.v3"
    1915        "marisa.chaotic.ninja/aya"
     
    175171}
    176172
    177 // Renders markdown with the given layout into html expanding all the macros
    178 func buildMarkdown(path string, w io.Writer, vars Vars) error {
    179         v, body, err := getVars(path, vars)
    180         extensions := blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs|blackfriday.Strikethrough|blackfriday.Footnotes
    181         if err != nil {
    182                 return err
    183         }
    184         content, err := render(body, v)
    185         if err != nil {
    186                 return err
    187         }
    188         v["content"] = string(blackfriday.Run([]byte(content),
    189                 blackfriday.WithExtensions(extensions),
    190         ))
    191         if w == nil {
    192                 out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html")))
    193                 if err != nil {
    194                         return err
    195                 }
    196                 defer out.Close()
    197                 w = out
    198         }
    199         return buildHTML(filepath.Join(AYADIR, v["layout"]), w, v)
    200 }
    201 
    202 // Renders hypertext file expanding all variable macros inside it
    203 func buildHTML(path string, w io.Writer, vars Vars) error {
    204         v, body, err := getVars(path, vars)
    205         if err != nil {
    206                 return err
    207         }
    208         if body, err = render(body, v); err != nil {
    209                 return err
    210         }
    211         tmpl, err := template.New("").Delims("<%", "%>").Parse(body)
    212         if err != nil {
    213                 return err
    214         }
    215         if w == nil {
    216                 f, err := os.Create(filepath.Join(PUBDIR, path))
    217                 if err != nil {
    218                         return err
    219                 }
    220                 defer f.Close()
    221                 w = f
    222         }
    223         return tmpl.Execute(w, vars)
    224 }
    225 
    226 // Renders .amber file into .html
    227 func buildAmber(path string, w io.Writer, vars Vars) error {
    228         v, body, err := getVars(path, vars)
    229         if err != nil {
    230                 return err
    231         }
    232         a := amber.New()
    233         if err := a.Parse(body); err != nil {
    234                 fmt.Println(body)
    235                 return err
    236         }
    237 
    238         t, err := a.Compile()
    239         if err != nil {
    240                 return err
    241         }
    242 
    243         htmlBuf := &bytes.Buffer{}
    244         if err := t.Execute(htmlBuf, v); err != nil {
    245                 return err
    246         }
    247 
    248         if body, err = render(string(htmlBuf.Bytes()), v); err != nil {
    249                 return err
    250         }
    251 
    252         if w == nil {
    253                 f, err := os.Create(filepath.Join(PUBDIR, renameExt(path, ".amber", ".html")))
    254                 if err != nil {
    255                         return err
    256                 }
    257                 defer f.Close()
    258                 w = f
    259         }
    260         _, err = io.WriteString(w, body)
    261         return err
    262 }
    263 
    264 // Compiles .gcss into .css
    265 func buildGCSS(path string, w io.Writer) error {
    266         f, err := os.Open(path)
    267         if err != nil {
    268                 return err
    269         }
    270         defer f.Close()
    271 
    272         if w == nil {
    273                 s := strings.TrimSuffix(path, ".gcss") + ".css"
    274                 css, err := os.Create(filepath.Join(PUBDIR, s))
    275                 if err != nil {
    276                         return err
    277                 }
    278                 defer css.Close()
    279                 w = css
    280         }
    281         _, err = gcss.Compile(w, f)
    282         return err
    283 }
    284 
    285 // Copies file as is from path to writer
    286 func buildRaw(path string, w io.Writer) error {
    287         in, err := os.Open(path)
    288         if err != nil {
    289                 return err
    290         }
    291         defer in.Close()
    292         if w == nil {
    293                 if out, err := os.Create(filepath.Join(PUBDIR, path)); err != nil {
    294                         return err
    295                 } else {
    296                         defer out.Close()
    297                         w = out
    298                 }
    299         }
    300         _, err = io.Copy(w, in)
    301         return err
    302 }
    303 
    304173// This function passes the files to build to their corresponding functions
    305174// As far as I'm aware, Markdown has three possible filename extensions,
  • trunk/version.go

    r78 r79  
    99var (
    1010        // Set to current tag
    11         Version = "v0.6.9"
     11        Version = "v0.7.0"
    1212        Time = time.Now()
    1313)
Note: See TracChangeset for help on using the changeset viewer.