diff options
| author | Nicolas Paul <n@nc0.fr> | 2023-04-27 17:50:42 +0200 |
|---|---|---|
| committer | Nicolas Paul <n@nc0.fr> | 2023-04-27 17:50:42 +0200 |
| commit | 0924a2f58236dd6b379a1c3fc16ea01cbaa35288 (patch) | |
| tree | 269a7fc9c36000bd827cdba0874827d9bae7353b | |
| parent | a4ac45fc59923e2e26ebfbf5ffd0a810745d1617 (diff) | |
Do not transform hidden files
Hidden files are prefixed with a dot
| -rw-r--r-- | crocc.go | 64 | ||||
| -rw-r--r-- | transformations.go | 47 |
2 files changed, 41 insertions, 70 deletions
@@ -12,6 +12,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "text/template" ) @@ -99,7 +100,7 @@ func main() { htmlTemplate = *template.Must(template.New("html-template").Parse(string(tp))) // Logic - if err := Crocc(in, ""); err != nil { + if err := filepath.WalkDir(in, Crocc); err != nil { log.Fatalf("unable to complete generation from %q: %v", in, err) } } @@ -108,50 +109,35 @@ func main() { // child corresponds to the path of a nested subdirectory, relative to the root. // For example, if the root is "src" and the child is "foo/bar", the function // will be applied to "src/foo/bar". -func Crocc(root string, child string) error { - outputPath := filepath.Join(*out, child) - files, err := os.ReadDir(root) - if err != nil { - return err +func Crocc(path string, d os.DirEntry, e error) error { + if e != nil { + return e } - for _, file := range files { - filename := file.Name() - - // Ignore template file - if filename == ".crocc.html" { - continue - } - - // If the file is a directory, create it in the output directory - if file.IsDir() { - if err := TransformDirectory(root, filename, outputPath); err != nil { - return err - } + // ignore the input directory itself and hidden files + if path == in || strings.HasPrefix(d.Name(), ".") { + return nil + } - if err := Crocc(filepath.Join(root, filename), filename); err != nil { - return err - } + o := strings.Replace(path, in, *out, 1) - continue - } - - // Copy non-Markdown files into the output directory - if filepath.Ext(filename) != ".md" && - filepath.Ext(filename) != ".markdown" && - filepath.Ext(filename) != ".mdown" && - filepath.Ext(filename) != ".Markdown" { - if err := TransformNonMarkdownFile(root, filename, outputPath); err != nil { - return err - } + // If the file is a directory, create it in the output directory + if d.IsDir() { + return TransformDirectory(o) + } - continue - } + // Copy non-Markdown files into the output directory + if filepath.Ext(path) != ".md" && + filepath.Ext(path) != ".markdown" && + filepath.Ext(path) != ".mdown" && + filepath.Ext(path) != ".Markdown" { + return TransformNonMarkdownFile(path, o) + } - // Transform Markdown files into HTML - if err := TransformMarkdownFile(root, filename, outputPath); err != nil { - return err - } + // If the file is a Markdown file, transform it into HTML + o = strings.TrimSuffix(o, filepath.Ext(o)) + ".html" + if err := TransformMarkdownFile(path, o); err != nil { + return err } return nil diff --git a/transformations.go b/transformations.go index ddf259d..14c57b3 100644 --- a/transformations.go +++ b/transformations.go @@ -7,8 +7,6 @@ package main import ( "log" "os" - "path/filepath" - "strings" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/html" @@ -17,61 +15,48 @@ import ( // TransformMarkdownFile simply copy a non-markdown file to the output // directory. -func TransformNonMarkdownFile(inputDir, inputFile, outputDir string) error { - inputPath := filepath.Join(inputDir, inputFile) - - input, err := os.ReadFile(inputPath) +func TransformNonMarkdownFile(i, o string) error { + input, err := os.ReadFile(i) if err != nil { return err } - outputPath := filepath.Join(outputDir, inputFile) - if err := os.WriteFile(outputPath, input, 0666); err != nil { + if err := os.WriteFile(o, input, 0666); err != nil { return err } - log.Printf("copied file %q to %q", inputPath, outputPath) + log.Printf("copied file %q to %q", i, o) return nil } // TransformDirectory creates a directory in the output directory. -func TransformDirectory(inputDir, inputFile, outputDir string) error { - outputPath := filepath.Join(outputDir, inputFile) - - if err := os.MkdirAll(outputPath, 0777); err != nil { +func TransformDirectory(o string) error { + if err := os.MkdirAll(o, 0777); err != nil { return err } - log.Printf("created directory %q", outputPath) - + log.Printf("created directory %q", o) return nil } // TransformMarkdownFile generates the corresponding HTML document from a // Markdown file. -func TransformMarkdownFile(inputDir, inputFile, outputDir string) error { - inputPath := filepath.Join(inputDir, inputFile) - - // The output file is the same as the input file, but with a different - // extension. - fn := strings.TrimSuffix(inputFile, filepath.Ext(inputFile)) + ".html" - outputPath := filepath.Join(outputDir, fn) - - contentRaw, err := os.ReadFile(inputPath) +func TransformMarkdownFile(i, o string) error { + raw, err := os.ReadFile(i) if err != nil { return err } // Parse front matter - fm, contentMD, err := ParseFrontMatter(contentRaw) + fm, md, err := ParseFrontMatter(raw) if err != nil { return err } // Skip hidden files unless -hidden is specified if fm.Hide && !*generateHidden { - log.Printf("skipped hidden file %q", inputPath) + log.Printf("skipped hidden file %q", i) return nil } @@ -82,23 +67,23 @@ func TransformMarkdownFile(inputDir, inputFile, outputDir string) error { parser.AutoHeadingIDs | parser.Footnotes | parser.SuperSubscript | parser.NoIntraEmphasis p := parser.NewWithExtensions(pExtensions) - doc := p.Parse(contentMD) + ast := p.Parse(md) htmlFlags := html.Smartypants | html.SmartypantsFractions | html.SmartypantsDashes | html.SmartypantsLatexDashes | html.HrefTargetBlank | html.LazyLoadImages renderer := html.NewRenderer(html.RendererOptions{Flags: htmlFlags}) - contentHTML := markdown.Render(doc, renderer) + html := markdown.Render(ast, renderer) - c, err := GenerateHTML(fm, string(contentHTML)) + c, err := GenerateHTML(fm, string(html)) if err != nil { return err } - if err := os.WriteFile(outputPath, c, 0666); err != nil { + if err := os.WriteFile(o, c, 0666); err != nil { return err } - log.Printf("generated file %q", outputPath) + log.Printf("generated file %q", o) return nil } |
