Skip to content

Commit 4a8d3de

Browse files
committed
chore: initialize shiroa project
1 parent 30e8ad5 commit 4a8d3de

File tree

9 files changed

+1609
-0
lines changed

9 files changed

+1609
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
*.pdf

book.typ

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#import "@preview/shiroa:0.2.0": *
3+
4+
#show: book
5+
6+
#book-meta(
7+
title: "shiroa",
8+
summary: [
9+
#prefix-chapter("sample-page.typ")[Hello, typst]
10+
],
11+
)
12+
13+
14+
15+
// re-export page template
16+
#import "/templates/page.typ": project
17+
#let book-page = project

ebook.typ

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import "@preview/shiroa:0.2.0": *
2+
3+
#import "/templates/ebook.typ"
4+
5+
#show: ebook.project.with(title: "typst-book", spec: "book.typ")
6+
7+
// set a resolver for inclusion
8+
#ebook.resolve-inclusion(it => include it)

mise.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[tools]
2+
typstyle = "0.13.17"
3+
"ubi:Myriad-Dreamin/shiroa" = "0.3.0"
4+
5+
[tasks]
6+
dev = "shiroa serve"
7+
build = "shiroa build"
8+
clean = "shiroa clean"
9+
format = { run = "typstyle --inplace .", alias = "fmt" }

sample-page.typ

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import "/book.typ": book-page
2+
3+
#show: book-page.with(title: "Hello, typst")
4+
5+
= Hello, typst
6+
7+
Sample page

templates/ebook.typ

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#import "@preview/shiroa:0.2.0": *
2+
#import "/templates/page.typ": part-style, project
3+
4+
#let _page-project = project
5+
6+
#let _resolve-inclusion-state = state("_resolve-inclusion", none)
7+
8+
#let resolve-inclusion(inc) = _resolve-inclusion-state.update(it => inc)
9+
10+
#let project(title: "", authors: (), spec: "", content) = {
11+
// Set document metadata early
12+
set document(
13+
author: authors,
14+
title: title,
15+
)
16+
17+
// Inherit from gh-pages
18+
show: _page-project
19+
20+
if title != "" {
21+
heading(title)
22+
}
23+
24+
context {
25+
let inc = _resolve-inclusion-state.final()
26+
external-book(spec: inc(spec))
27+
28+
let mt = book-meta-state.final()
29+
let styles = (inc: inc, part: part-style, chapter: it => it)
30+
31+
if mt != none {
32+
mt.summary.map(it => visit-summary(it, styles)).sum()
33+
}
34+
}
35+
36+
content
37+
}

templates/page.typ

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

templates/theme-style.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
[light]
3+
color-scheme = "light"
4+
main-color = "#000"
5+
dash-color = "#20609f"
6+
code-theme = ""
7+
8+
[rust]
9+
color-scheme = "light"
10+
main-color = "#262625"
11+
dash-color = "#2b79a2"
12+
code-theme = ""
13+
14+
[coal]
15+
color-scheme = "dark"
16+
main-color = "#98a3ad"
17+
dash-color = "#2b79a2"
18+
code-theme = "tokyo-night.tmTheme"
19+
20+
[navy]
21+
color-scheme = "dark"
22+
main-color = "#bcbdd0"
23+
dash-color = "#2b79a2"
24+
code-theme = "tokyo-night.tmTheme"
25+
26+
[ayu]
27+
color-scheme = "dark"
28+
main-color = "#c5c5c5"
29+
dash-color = "#0096cf"
30+
code-theme = "tokyo-night.tmTheme"

0 commit comments

Comments
 (0)