diff options
| author | msunet <msunet@shellblade.net> | 2021-12-03 20:56:16 -0800 |
|---|---|---|
| committer | msunet <msunet@shellblade.net> | 2021-12-03 20:56:16 -0800 |
| commit | 5d6ec23f261452a138ab081303b19eaf454006ee (patch) | |
| tree | b77c26add13af014bc39e4cdd64eeebe7fedafa8 | |
| parent | c0bd17799c1ae1f83dc4bb3ebf5407eac263cfc3 (diff) | |
Simplify file processing.
| -rw-r--r-- | webgen.janet | 23 |
1 files changed, 5 insertions, 18 deletions
diff --git a/webgen.janet b/webgen.janet index 1ba3d5a..1b21544 100644 --- a/webgen.janet +++ b/webgen.janet | |||
| @@ -39,24 +39,12 @@ | |||
| 39 | (string/replace-all "${ROOT}/" (string/repeat "../" (- (nesting-levels path) 1)) str)) | 39 | (string/replace-all "${ROOT}/" (string/repeat "../" (- (nesting-levels path) 1)) str)) |
| 40 | 40 | ||
| 41 | (defn process-html | 41 | (defn process-html |
| 42 | "Process the given HTML contents, applying the website's template and | 42 | "Process the given HTML contents, applying the website's template." |
| 43 | applying variable substitution." | ||
| 44 | [template variables path contents] | 43 | [template variables path contents] |
| 45 | (substitute-variables variables path | 44 | (substitute-variables variables path |
| 46 | (string/replace "${CONTENTS}" contents | 45 | (string/replace "${CONTENTS}" contents |
| 47 | template))) | 46 | template))) |
| 48 | 47 | ||
| 49 | (defn process-markdown-file | ||
| 50 | "Process a markdown file. Return the resulting HTML." | ||
| 51 | [process-html path] | ||
| 52 | (process-html path (markdown-file-to-html path))) | ||
| 53 | |||
| 54 | (defn process-file | ||
| 55 | "Process a file with the given contents processor. The process takes a file | ||
| 56 | path and the file's contents." | ||
| 57 | [process path] | ||
| 58 | (process path (filesystem/read-file path))) | ||
| 59 | |||
| 60 | (defn process-files | 48 | (defn process-files |
| 61 | "Process the files in the source directory and write the results to the | 49 | "Process the files in the source directory and write the results to the |
| 62 | destination directory." | 50 | destination directory." |
| @@ -72,10 +60,9 @@ | |||
| 72 | (def ext (path/ext src-filepath)) | 60 | (def ext (path/ext src-filepath)) |
| 73 | (def processed-contents | 61 | (def processed-contents |
| 74 | (match ext | 62 | (match ext |
| 75 | ".css" (process-file (partial substitute-variables variables) src-filepath) | 63 | ".css" (substitute-variables variables src-filepath (filesystem/read-file src-filepath)) |
| 76 | ".html" (process-file (partial process-html template variables) src-filepath) | 64 | ".html" (substitute-variables variables src-filepath process-html template (filesystem/read-file src-filepath)) |
| 77 | ".md" (process-markdown-file (partial process-html template variables) src-filepath) | 65 | ".md" (substitute-variables variables src-filepath process-html template (markdown-file-to-html src-filepath)) |
| 78 | ".markdown" (process-markdown-file (partial process-html template variables) src-filepath) | ||
| 79 | _ nil)) # nil means we copy the file as is. | 66 | _ nil)) # nil means we copy the file as is. |
| 80 | (var dst-filepath (string/replace src-dir build-dir src-filepath)) | 67 | (var dst-filepath (string/replace src-dir build-dir src-filepath)) |
| 81 | (set dst-filepath | 68 | (set dst-filepath |
| @@ -85,7 +72,7 @@ | |||
| 85 | _ dst-filepath)) | 72 | _ dst-filepath)) |
| 86 | (if processed-contents | 73 | (if processed-contents |
| 87 | (filesystem/write-file dst-filepath processed-contents) | 74 | (filesystem/write-file dst-filepath processed-contents) |
| 88 | (filesystem/copy-file src-filepath dst-filepath)))) | 75 | (filesystem/copy-file src-filepath dst-filepath)))) |
| 89 | 76 | ||
| 90 | (defn read-map-file | 77 | (defn read-map-file |
| 91 | "Read key-value pairs from a file. The file should be a text file with lines | 78 | "Read key-value pairs from a file. The file should be a text file with lines |
