all repos — hastyscribe @ 91464202176c8ad22de55d8544de0e851951b6c5

A professional markdown compiler.

Merge pull request #79 from tomidery/more-public-functions

Fabio Cevasco h3rald@h3rald.com
Tue, 06 Apr 2021 22:21:50 +0200
commit

91464202176c8ad22de55d8544de0e851951b6c5

parent

67ce5735917a52024ec92c71f85d7a90abe04284

2 files changed, 16 insertions(+), 9 deletions(-)

jump to
M src/hastyscribe.nimsrc/hastyscribe.nim

@@ -132,10 +132,6 @@ let imgrep = img.replace("\"" & img_file & "\"", "\"" & imgcontent & "\"")

doc = doc.replace(img, imgrep) hs.document = doc -proc add_jump_to_top_links(hs: var HastyScribe) = - hs.document = hs.document.replacef(peg"{'</h' [23456] '>'}", "<a href=\"#document-top\" title=\"Go to top\"></a>$1") - - proc embed_fonts(): string= let fonts = @[ create_font_face(hastyscribe_font, "HastyScribe", "normal", 400),

@@ -152,7 +148,7 @@ create_font_face(sourcesanspro_boldit_font, "Source Sans Pro", "italic", 700)

] return style_tag(fonts.join); -proc preprocess(hs: var HastyScribe, document, dir: string, offset = 0): string +proc preprocess*(hs: var HastyScribe, document, dir: string, offset = 0): string proc applyHeadingOffset(contents: string, offset: int): string = if offset == 0:

@@ -325,7 +321,7 @@ discard anchor.match(peg_anchor, matches)

var id = matches[0] result = result.replace(anchor, " <a id=\""&id&"\"></a>") -proc preprocess(hs: var HastyScribe, document, dir: string, offset = 0): string = +proc preprocess*(hs: var HastyScribe, document, dir: string, offset = 0): string = result = hs.parse_transclusions(document, dir, offset) result = hs.parse_fields(result) result = hs.parse_snippets(result)

@@ -444,7 +440,7 @@ $js

</body>""" % ["title_tag", title_tag, "header_tag", header_tag, "author", metadata.author, "author_footer", author_footer, "date", timeinfo.format("MMMM d, yyyy"), "toc", toc, "main_css_tag", main_css_tag, "user_css_tag", user_css_tag, "headings", headings, "body", hs.document, "fonts_css_tag", embed_fonts(), "internal_css_tag", metadata.css, "watermark_css_tag", watermark_css_tag, "js", user_js_tag] hs.embed_images(dir) - hs.add_jump_to_top_links() + hs.document = add_jump_to_top_links(hs.document) return hs.document proc compile*(hs: var HastyScribe, input_file: string) =
M src/hastyscribepkg/utils.nimsrc/hastyscribepkg/utils.nim

@@ -10,6 +10,9 @@

proc style_tag*(css: string): string = result = "<style>$1</style>" % [css] +proc style_link_tag*(css: string): string = + result = "<link rel=\"stylesheet\" href=\"$1\"/>" % [css] + proc encode_image*(contents, format: string): string = let enc_contents = contents.encode return "data:image/$format;base64,$enc_contents" % ["format", format, "enc_contents", enc_contents]

@@ -33,11 +36,19 @@ else:

let img = imgfile.encode_image_file(imgfile.image_format) result = (watermark_style % [img]).style_tag +proc add_jump_to_top_links*(document: string): string = + result = document.replacef(peg"{'</h' [23456] '>'}", "<a href=\"#document-top\" title=\"Go to top\"></a>$1") + proc encode_font*(font, format: string): string = let enc_contents = font.encode return "data:application/$format;charset=utf-8;base64,$enc_contents" % ["format", format, "enc_contents", enc_contents] -proc create_font_face*(font, family, style: string, weight: int): string= +proc create_font_face*(font, family, style: string, weight: int, embed=true): string= + var font_src = "" + if embed: + font_src = encode_font(font, "x-font-woff") + else: + font_src = font return """ @font-face { font-family:"$family";

@@ -47,6 +58,6 @@ font-weight:$weight;

-webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; } - """ % ["family", family, "font", encode_font(font, "x-font-woff"), "style", style, "weight", $weight] + """ % ["family", family, "font", font_src, "style", style, "weight", $weight]