Skip to content

Commit 5a7db3d

Browse files
committed
refactor: make export options into a struct
1 parent cf3bef4 commit 5a7db3d

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

main.go

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ type page struct {
2222
text string
2323
}
2424

25+
type exportOptions struct {
26+
graphPath string
27+
blogFolder string
28+
assetsRelativePath string
29+
webAssetsPathPrefix string
30+
unquotedProperties []string
31+
}
32+
2533
/*
2634
findMatchingFiles finds all files in rootPath that contain substring
2735
ignoreRegexp is an expression that is evaluated on **relative** path of files within the graph (e.g. `.git/HEAD` or `logseq/.bkp/something.md`) if it matches, the file is not processed
@@ -63,52 +71,67 @@ func findMatchingFiles(appFS afero.Fs, rootPath string, substring string, ignore
6371
return result, nil
6472
}
6573

66-
func main() {
74+
/*
75+
parseOptions produce a valid exportOptions object
76+
if a mandatory argument is missing, parseOptions will print error message, program usage and exits with os.Exit(1)
77+
*/
78+
func parseOptions() exportOptions {
6779
graphPath := flag.String("graphPath", "", "[MANDATORY] Folder where all public pages are exported.") // TODO rename graphPath -> graphFolder or maybe logseqFolder
6880
blogFolder := flag.String("blogFolder", "", "[MANDATORY] Folder where this program creates a new subfolder with public logseq pages.")
6981
assetsRelativePath := flag.String("assetsRelativePath", "logseq-images", "relative path within blogFolder where the assets (images) should be stored (e.g. 'static/images/logseq'). Default is `logseq-images`")
7082
webAssetsPathPrefix := flag.String("webAssetsPathPrefix", "/logseq-images", "path that the images are going to be served on on the web (e.g. '/public/images/logseq'). Default is `/logseq-images`")
71-
unquotedProperties := flag.String("unquotedProperties", "", "comma-separated list of logseq page properties that won't be quoted in the markdown front matter, e.g. 'date,public,slug")
83+
rawUnquotedProperties := flag.String("unquotedProperties", "", "comma-separated list of logseq page properties that won't be quoted in the markdown front matter, e.g. 'date,public,slug")
7284
flag.Parse()
7385
if *graphPath == "" || *blogFolder == "" {
7486
log.Println("mandatory argument is missing")
7587
flag.Usage()
7688
os.Exit(1)
7789
}
90+
unquotedProperties := parseUnquotedProperties(*rawUnquotedProperties)
91+
return exportOptions{
92+
graphPath: *graphPath,
93+
blogFolder: *blogFolder,
94+
assetsRelativePath: *assetsRelativePath,
95+
webAssetsPathPrefix: *webAssetsPathPrefix,
96+
unquotedProperties: unquotedProperties,
97+
}
98+
}
99+
100+
func main() {
78101
appFS := afero.NewOsFs()
79-
publicFiles, err := findMatchingFiles(appFS, *graphPath, "public::", regexp.MustCompile(`^(logseq|.git)/`))
102+
options := parseOptions()
103+
publicFiles, err := findMatchingFiles(appFS, options.graphPath, "public::", regexp.MustCompile(`^(logseq|.git)/`))
80104
if err != nil {
81105
log.Fatalf("Error during walking through a folder %v", err)
82106
}
83-
puq := parseUnquotedProperties(*unquotedProperties)
84107
for _, publicFile := range publicFiles {
85-
err = exportPublicPage(appFS, publicFile, *webAssetsPathPrefix, *blogFolder, *assetsRelativePath, puq)
108+
err = exportPublicPage(appFS, publicFile, options)
86109
if err != nil {
87110
log.Fatalf("Error when exporting page %q: %v", publicFile, err)
88111
}
89112
}
90113
}
91114

92-
func exportPublicPage(appFS afero.Fs, publicFile string, webAssetsPathPrefix string, blogFolder string, assetsRelativePath string, unquotedProperties []string) error {
115+
func exportPublicPage(appFS afero.Fs, publicFile string, options exportOptions) error {
93116
srcContent, err := afero.ReadFile(appFS, publicFile)
94117
if err != nil {
95118
return fmt.Errorf("reading the %q file failed: %v", publicFile, err)
96119
}
97120
_, name := filepath.Split(publicFile)
98121
page := parsePage(name, string(srcContent))
99-
result := transformPage(page, webAssetsPathPrefix)
100-
assetFolder := filepath.Join(blogFolder, assetsRelativePath)
122+
result := transformPage(page, options.webAssetsPathPrefix)
123+
assetFolder := filepath.Join(options.blogFolder, options.assetsRelativePath)
101124
err = copyAssets(appFS, publicFile, assetFolder, result.assets)
102125
if err != nil {
103126
return fmt.Errorf("copying assets for page %q failed: %v", publicFile, err)
104127
}
105-
dest := filepath.Join(blogFolder, result.filename)
128+
dest := filepath.Join(options.blogFolder, result.filename)
106129
folder, _ := filepath.Split(dest)
107130
err = appFS.MkdirAll(folder, os.ModePerm)
108131
if err != nil {
109132
return fmt.Errorf("creating parent directory for %q failed: %v", dest, err)
110133
}
111-
outputFileContent := render(result, unquotedProperties)
134+
outputFileContent := render(result, options.unquotedProperties)
112135
err = afero.WriteFile(appFS, dest, []byte(outputFileContent), 0644)
113136
if err != nil {
114137
return fmt.Errorf("copying file %q failed: %v", dest, err)

0 commit comments

Comments
 (0)