all repos — hastyscribe @ df0b0b7f7dc6bc0289e693b6600351dd6be05880

A professional markdown compiler.

Merge pull request #95 from ZoomRmc/cssmin

Fabio Cevasco h3rald@h3rald.com
Wed, 20 Sep 2023 15:42:26 -0400
commit

df0b0b7f7dc6bc0289e693b6600351dd6be05880

parent

d807b960d8903265f383a84b6ca5f75efc1c6e93

4 files changed, 30 insertions(+), 2 deletions(-)

jump to
M README.mdREADME.md

@@ -25,6 +25,7 @@ - **--notoc** causes HastyScribe to output HTML documents _without_ automatically generating a Table of Contents at the start.

- **--noembed** causes styles and images not to be embedded. - **--fragment** causes HastyScribe to output just an HTML fragment instead of a full document, without embedding any image, font or stylesheet. - **--iso** enables HastyScribe to use the ISO 8601 date format (e.g., 2000-12-31) in the footer of the generated HTML documents. + - **--minify-css** uses an unsophisticated minifier on the built-in stylesheet before embedding it into HTML. Ignored when combined with `--noembed`. - **--no-clobber** or **-n** prevents HastyScribe from overwriting existing files. If a file with the same name already exists, HastyScribe will issue a warning and will not overwrite it. - **--help** causes HastyScribe to display the usage information and quit.
M doc/-usage.mddoc/-usage.md

@@ -19,6 +19,7 @@ * [\-\-watermark=<file>](class:opt) causes {{hs}} to embed and display an image as a watermark throughout the document.

* [\-\-notoc](class:opt) causes {{hs}} to output HTML documents _without_ automatically generating a Table of Contents at the start. * [\-\-noembed](class:opt) causes styles and images not to be embedded. * [\-\-fragment](class:opt) causes {{hs}} to output just an HTML fragment instead of a full document, without embedding any image, font or stylesheet. + * [\-\-minify-css](class:opt) uses an unsophisticated minifier on the built-in stylesheet before embedding it into HTML. Ignored when combined with [\-\-noembed](class:opt). * [\-\-iso](class:opt) enables {{hs}} to use the ISO 8601 date format (e.g., 2000-12-31) in the footer of the generated HTML documents. * [\-\-no-clobber](class:opt) or [\-n](class:opt) prevents {{hs}} from overwriting existing files. If a file with the same name already exists, {{hs}} will issue a warning and will not overwrite it. * [\-\-help](class:opt) causes {{hs}} to display the usage information and quit.
M src/hastyscribe.nimsrc/hastyscribe.nim

@@ -44,6 +44,7 @@ watermark*: string

fragment*: bool = false embed*: bool = true iso*: bool = false + minifycss*: bool = false noclobber*: bool = false outputToDir*: bool = false processingMultiple: bool = false

@@ -59,6 +60,7 @@ fields: HastyFields

snippets: HastySnippets macros: HastyMacros document: string + hastyStylesheet: string iconStyles: HastyIconStyles noteStyles: HastyNoteStyles badgeStyles: HastyBadgeStyles

@@ -94,7 +96,18 @@ result["month-name-abbr"] = now.format("MMM")

result["timezone-offset"] = now.format("zzz") proc newHastyScribe*(options: HastyOptions, fields: HastyFields): HastyScribe = - return HastyScribe(options: options, fields: initFields(fields), snippets: initTable[string, string](), macros: initTable[string, string](), document: "") + HastyScribe( + options: options, + fields: initFields(fields), + snippets: initTable[string, string](), + macros: initTable[string, string](), + document: "", + hastyStylesheet: ( + if not options.embed: "" + elif options.minifycss: stylesheet.minifyCss() + else: stylesheet + ), + ) # Utility Procedures

@@ -427,7 +440,7 @@ header_tag = if metadata.title == "": "" else:

"<div id=\"header\"><h1>" & metadata.title & "</h1></div>" (main_css_tag, optional_css_tag) = if hs.options.embed: - (stylesheet.style_tag, hs.create_optional_css(hs.document)) + (hs.hastyStylesheet.style_tag, hs.create_optional_css(hs.document)) else: ("", "")

@@ -575,6 +588,7 @@ --noembed If specified, styles and images will not be embedded.

--fragment If specified, an HTML fragment will be generated, without embedding images or stylesheets. --iso Use ISO 8601 date format (e.g., 2000-12-31) in the footer. + --minify-css, Minify the built-in stylesheet before embedding. --no-clobber, -n Do not overwrite existing files. --help, -h Display the usage information. --version, -v Print version and exit."""

@@ -623,6 +637,9 @@ options.fragment = true

of "iso": noVal() options.iso = true + of "minify-css": + noVal() + options.minifycss = true of "n", "no-clobber", "noclobber": noVal() options.noclobber = true
M src/hastyscribepkg/utils.nimsrc/hastyscribepkg/utils.nim

@@ -63,3 +63,12 @@ hashBytes = cast[array[sizeof(Hash), byte]](hash(dir))

uniquePrefix = encode(hashBytes, safe=true) baseName & '_' & uniquePrefix else: baseName + +proc minifyCss*(css: string): string = + css.parallelReplace([ + (peg" '/*' @ '*/'", ""), + (peg" {\w} \s* {[ \>\< ]} \s* {\w / '*'} ", "$1$2$3" ), + (peg" \s* { [ \,\{\}\[\]\:\; ] } \s* ", "$1"), + (peg" \s+ ", " "), + (peg""" ')' \s* \" """, ")\"" ), + ])