|
| 1 | +// This is important for shiroa to produce a responsive layout |
| 2 | +// and multiple targets. |
| 3 | +#import "@preview/shiroa:0.2.0": ( |
| 4 | + get-page-width, is-html-target, is-pdf-target, is-web-target, plain-text, |
| 5 | + shiroa-sys-target, target, templates, |
| 6 | +) |
| 7 | +#import templates: * |
| 8 | +#import "@preview/zebraw:0.4.5": zebraw-html, zebraw-init |
| 9 | + |
| 10 | +// Metadata |
| 11 | +#let page-width = get-page-width() |
| 12 | +#let is-html-target = is-html-target() |
| 13 | +#let is-pdf-target = is-pdf-target() |
| 14 | +#let is-web-target = is-web-target() |
| 15 | +#let sys-is-html-target = ("target" in dictionary(std)) |
| 16 | + |
| 17 | +/// Creates an embedded block typst frame. |
| 18 | +#let div-frame(content, attrs: (:)) = html.elem( |
| 19 | + "div", |
| 20 | + html.frame(content), |
| 21 | + attrs: attrs, |
| 22 | +) |
| 23 | + |
| 24 | +// Theme (Colors) |
| 25 | +#let ( |
| 26 | + style: theme-style, |
| 27 | + is-dark: is-dark-theme, |
| 28 | + is-light: is-light-theme, |
| 29 | + main-color: main-color, |
| 30 | + dash-color: dash-color, |
| 31 | + code-extra-colors: code-extra-colors, |
| 32 | +) = book-theme-from(toml("theme-style.toml"), xml: it => xml(it)) |
| 33 | + |
| 34 | +// Fonts |
| 35 | +#let main-font = ( |
| 36 | + "Charter", |
| 37 | + "Source Han Serif SC", |
| 38 | + // "Source Han Serif TC", |
| 39 | + // shiroa's embedded font |
| 40 | + "Libertinus Serif", |
| 41 | +) |
| 42 | +#let code-font = ( |
| 43 | + "BlexMono Nerd Font Mono", |
| 44 | + // shiroa's embedded font |
| 45 | + "DejaVu Sans Mono", |
| 46 | +) |
| 47 | + |
| 48 | +// Sizes |
| 49 | +#let main-size = if is-web-target { |
| 50 | + 16pt |
| 51 | +} else { |
| 52 | + 10.5pt |
| 53 | +} |
| 54 | +#let heading-sizes = (26pt, 22pt, 14pt, 12pt, main-size) |
| 55 | +#let list-indent = 0.5em |
| 56 | + |
| 57 | +/// The project function defines how your document looks. |
| 58 | +/// It takes your content and some metadata and formats it. |
| 59 | +/// Go ahead and customize it to your liking! |
| 60 | +#let project(title: "Typst Book", authors: (), kind: "page", body) = { |
| 61 | + // set basic document metadata |
| 62 | + set document( |
| 63 | + author: authors, |
| 64 | + title: title, |
| 65 | + ) if not is-pdf-target |
| 66 | + |
| 67 | + // set web/pdf page properties |
| 68 | + set page( |
| 69 | + numbering: none, |
| 70 | + number-align: center, |
| 71 | + width: page-width, |
| 72 | + ) if not (sys-is-html-target or is-html-target) |
| 73 | + |
| 74 | + // remove margins for web target |
| 75 | + set page( |
| 76 | + margin: ( |
| 77 | + // reserved beautiful top margin |
| 78 | + top: 20pt, |
| 79 | + // reserved for our heading style. |
| 80 | + // If you apply a different heading style, you may remove it. |
| 81 | + left: 20pt, |
| 82 | + // Typst is setting the page's bottom to the baseline of the last line of text. So bad :(. |
| 83 | + bottom: 0.5em, |
| 84 | + // remove rest margins. |
| 85 | + rest: 0pt, |
| 86 | + ), |
| 87 | + height: auto, |
| 88 | + ) if is-web-target and not is-html-target |
| 89 | + |
| 90 | + // Set main text |
| 91 | + set text( |
| 92 | + font: main-font, |
| 93 | + size: main-size, |
| 94 | + fill: main-color, |
| 95 | + lang: "en", |
| 96 | + ) |
| 97 | + |
| 98 | + // Set main spacing |
| 99 | + set enum( |
| 100 | + indent: list-indent * 0.618, |
| 101 | + body-indent: list-indent, |
| 102 | + ) |
| 103 | + set list( |
| 104 | + indent: list-indent * 0.618, |
| 105 | + body-indent: list-indent, |
| 106 | + ) |
| 107 | + set par(leading: 0.7em) |
| 108 | + set block(spacing: 0.7em * 1.5) |
| 109 | + |
| 110 | + // Set text, spacing for headings |
| 111 | + // Render a dash to hint headings instead of bolding it as well if it's for web. |
| 112 | + show heading: set text(weight: "regular") if is-web-target |
| 113 | + show heading: it => { |
| 114 | + let it = { |
| 115 | + set text(size: heading-sizes.at(it.level)) |
| 116 | + if is-web-target { |
| 117 | + heading-hash(it, hash-color: dash-color) |
| 118 | + } |
| 119 | + it |
| 120 | + } |
| 121 | + |
| 122 | + block( |
| 123 | + spacing: 0.7em * 1.5 * 1.2, |
| 124 | + below: 0.7em * 1.2, |
| 125 | + it, |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + // link setting |
| 130 | + show link: set text(fill: dash-color) |
| 131 | + |
| 132 | + // math setting |
| 133 | + show math.equation: set text(weight: 400) |
| 134 | + show math.equation: context if shiroa-sys-target() == "html" { |
| 135 | + div-frame.with(attrs: ( |
| 136 | + "style": "display: flex; justify-content: center; overflow-x: auto;", |
| 137 | + )) |
| 138 | + } else { |
| 139 | + it => it |
| 140 | + } |
| 141 | + |
| 142 | + /// HTML code block supported by zebraw. |
| 143 | + show: if is-dark-theme { |
| 144 | + zebraw-init.with( |
| 145 | + // should vary by theme |
| 146 | + background-color: if code-extra-colors.bg != none { |
| 147 | + (code-extra-colors.bg, code-extra-colors.bg) |
| 148 | + }, |
| 149 | + highlight-color: rgb("#3d59a1"), |
| 150 | + comment-color: rgb("#394b70"), |
| 151 | + lang-color: rgb("#3d59a1"), |
| 152 | + lang: false, |
| 153 | + ) |
| 154 | + } else { |
| 155 | + zebraw-init.with(lang: false) |
| 156 | + } |
| 157 | + |
| 158 | + // code block setting |
| 159 | + set raw(theme: theme-style.code-theme) if theme-style.code-theme.len() > 0 |
| 160 | + show raw: set text(font: code-font) |
| 161 | + show raw.where(block: true): it => context if shiroa-sys-target() == "paged" { |
| 162 | + rect( |
| 163 | + width: 100%, |
| 164 | + inset: (x: 4pt, y: 5pt), |
| 165 | + radius: 4pt, |
| 166 | + fill: code-extra-colors.bg, |
| 167 | + [ |
| 168 | + #set text(fill: code-extra-colors.fg) if code-extra-colors.fg != none |
| 169 | + #set par(justify: false) |
| 170 | + // #place(right, text(luma(110), it.lang)) |
| 171 | + #it |
| 172 | + ], |
| 173 | + ) |
| 174 | + } else { |
| 175 | + set text(fill: code-extra-colors.fg) if code-extra-colors.fg != none |
| 176 | + set par(justify: false) |
| 177 | + zebraw-html( |
| 178 | + block-width: 100%, |
| 179 | + line-width: 100%, |
| 180 | + wrap: false, |
| 181 | + it, |
| 182 | + ) |
| 183 | + } |
| 184 | + |
| 185 | + // Main body. |
| 186 | + set par(justify: true) |
| 187 | + |
| 188 | + body |
| 189 | +} |
| 190 | + |
| 191 | +#let part-style = heading |
0 commit comments