Skip to content

Commit 47cfa17

Browse files
committed
Basic css minifier for the built-in stylesheet
Runs on `pegs.multireplace` and is very basic: deals only with whitespace and comments. Doesn't know anything about quoted strings in CSS.
1 parent d807b96 commit 47cfa17

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Where:
2525
- **--noembed** causes styles and images not to be embedded.
2626
- **--fragment** causes HastyScribe to output just an HTML fragment instead of a full document, without embedding any image, font or stylesheet.
2727
- **--iso** enables HastyScribe to use the ISO 8601 date format (e.g., 2000-12-31) in the footer of the generated HTML documents.
28+
- **--minify-css** uses an unsophisticated minifier on the built-in stylesheet before embedding it into HTML. Ignored when combined with `--noembed`.
2829
- **--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.
2930
- **--help** causes HastyScribe to display the usage information and quit.
3031

doc/-usage.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Where:
1919
* [\-\-notoc](class:opt) causes {{hs}} to output HTML documents _without_ automatically generating a Table of Contents at the start.
2020
* [\-\-noembed](class:opt) causes styles and images not to be embedded.
2121
* [\-\-fragment](class:opt) causes {{hs}} to output just an HTML fragment instead of a full document, without embedding any image, font or stylesheet.
22+
* [\-\-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).
2223
* [\-\-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.
2324
* [\-\-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.
2425
* [\-\-help](class:opt) causes {{hs}} to display the usage information and quit.

src/hastyscribe.nim

+19-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type
4444
fragment*: bool = false
4545
embed*: bool = true
4646
iso*: bool = false
47+
minifycss*: bool = false
4748
noclobber*: bool = false
4849
outputToDir*: bool = false
4950
processingMultiple: bool = false
@@ -59,6 +60,7 @@ type
5960
snippets: HastySnippets
6061
macros: HastyMacros
6162
document: string
63+
hastyStylesheet: string
6264
iconStyles: HastyIconStyles
6365
noteStyles: HastyNoteStyles
6466
badgeStyles: HastyBadgeStyles
@@ -94,7 +96,18 @@ proc initFields(fields: HastyFields): HastyFields {.gcsafe.} =
9496
result["timezone-offset"] = now.format("zzz")
9597

9698
proc newHastyScribe*(options: HastyOptions, fields: HastyFields): HastyScribe =
97-
return HastyScribe(options: options, fields: initFields(fields), snippets: initTable[string, string](), macros: initTable[string, string](), document: "")
99+
HastyScribe(
100+
options: options,
101+
fields: initFields(fields),
102+
snippets: initTable[string, string](),
103+
macros: initTable[string, string](),
104+
document: "",
105+
hastyStylesheet: (
106+
if not options.embed: ""
107+
elif options.minifycss: stylesheet.minifyCss()
108+
else: stylesheet
109+
),
110+
)
98111

99112
# Utility Procedures
100113

@@ -427,7 +440,7 @@ proc compileDocument*(hs: var HastyScribe, input, dir: string): string {.discard
427440
"<div id=\"header\"><h1>" & metadata.title & "</h1></div>"
428441

429442
(main_css_tag, optional_css_tag) = if hs.options.embed:
430-
(stylesheet.style_tag, hs.create_optional_css(hs.document))
443+
(hs.hastyStylesheet.style_tag, hs.create_optional_css(hs.document))
431444
else:
432445
("", "")
433446

@@ -575,6 +588,7 @@ when isMainModule:
575588
--fragment If specified, an HTML fragment will be generated, without
576589
embedding images or stylesheets.
577590
--iso Use ISO 8601 date format (e.g., 2000-12-31) in the footer.
591+
--minify-css, Minify the built-in stylesheet before embedding.
578592
--no-clobber, -n Do not overwrite existing files.
579593
--help, -h Display the usage information.
580594
--version, -v Print version and exit."""
@@ -623,6 +637,9 @@ when isMainModule:
623637
of "iso":
624638
noVal()
625639
options.iso = true
640+
of "minify-css":
641+
noVal()
642+
options.minifycss = true
626643
of "n", "no-clobber", "noclobber":
627644
noVal()
628645
options.noclobber = true

src/hastyscribepkg/utils.nim

+9
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ proc makeFNameUnique*(baseName, dir: string): string =
6363
uniquePrefix = encode(hashBytes, safe=true)
6464
baseName & '_' & uniquePrefix
6565
else: baseName
66+
67+
proc minifyCss*(css: string): string =
68+
css.parallelReplace([
69+
(peg" '/*' @ '*/'", ""),
70+
(peg" {\w} \s* {[ \>\< ]} \s* {\w / '*'} ", "$1$2$3" ),
71+
(peg" \s* { [ \,\{\}\[\]\:\; ] } \s* ", "$1"),
72+
(peg" \s+ ", " "),
73+
(peg""" ')' \s* \" """, ")\"" ),
74+
])

0 commit comments

Comments
 (0)