OILS / doctools / cmark.sh View on Github | oils.pub

237 lines, 46 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# doctools/cmark.sh <function name>
5#
6# Example:
7# doctools/cmark.sh download
8# doctools/cmark.sh extract
9# doctools/cmark.sh build
10# doctools/cmark.sh make-symlink
11# doctools/cmark.sh demo-ours # smoke test
12
13set -o nounset
14set -o pipefail
15set -o errexit
16
17REPO_ROOT=$(cd $(dirname $0)/.. && pwd)
18readonly REPO_ROOT
19
20readonly TAR_DIR=$REPO_ROOT/_cache
21
22readonly CMARK_VERSION=0.29.0
23readonly URL="https://github.com/commonmark/cmark/archive/$CMARK_VERSION.tar.gz"
24
25# 5/2020: non-hermetic dependency broke with Python 3 SyntaxError! Gah! TODO:
26# make this hermetic.
27#
28# https://pypi.org/project/Pygments/#history
29#
30# 7/2023: Download the wheel file
31# doctools/oils_doc.py OPTIONALLY uses this
32#
33# It's only used in the blog, so let's just put it in the oilshell.org repo,
34# not the oil repo
35#
36# 12/2024: I want a Markdown highlighter for doc/ul-table.md. It will look
37# nicer.
38
39download-old-pygments() {
40 wget --directory-prefix _tmp --no-clobber \
41 'https://files.pythonhosted.org/packages/be/39/32da3184734730c0e4d3fa3b2b5872104668ad6dc1b5a73d8e477e5fe967/Pygments-2.5.2-py2.py3-none-any.whl'
42}
43
44demo-theirs() {
45 echo '*hi*' | cmark
46}
47
48cmark-py() {
49 PYTHONPATH='.:vendor' doctools/cmark.py "$@"
50}
51
52demo-ours() {
53 export PYTHONPATH=.
54
55 echo '*hi*' | cmark-py
56
57 # This translates to <code class="language-sh"> which is cool.
58 #
59 # We could do syntax highlighting in JavaScript, or simply post-process HTML
60
61 cmark-py <<'EOF'
62```sh
63code
64block
65```
66
67```oil
68code
69block
70```
71EOF
72
73 # The $ syntax can be a little language.
74 #
75 # $oil-issue
76 # $cross-ref
77 # $blog-tag
78 # $oil-source-file
79 # $oil-commit
80
81 cmark-py <<'EOF'
82[click here]($xref:re2c)
83EOF
84
85 # Hm for some reason it gets rid of the blank lines in HTML. When rendering
86 # to text, we would have to indent and insert blank lines? I guess we can
87 # parse <p> and wrap it.
88
89 cmark-py <<'EOF'
90Test spacing out:
91
92 echo one
93 echo two
94
95Another paragraph with `code`.
96EOF
97}
98
99demo-quirks() {
100 ### Cases that came from writing ul-table
101
102 export PYTHONPATH=.
103
104 cmark-py --common-mark <<'EOF'
1051. what `<table>`
106EOF
107
108 # Very annoying: list items can't be empty
109 # <span />
110 cmark-py --common-mark <<'EOF'
111<table>
112
113- thead
114 - <!-- list item can't be empty -->
115 - Proc
116 - Func
117
118</table>
119EOF
120
121 cmark-py --common-mark <<'EOF'
122- <tr-attrs class=foo /> text required here
123 - one
124 - two
125EOF
126
127cmark-py --common-mark <<'EOF'
128- tr <tr-attrs class=foo />
129 - one
130 - two
131EOF
132
133 # Weird case - the `proc` is sometimes not expanded to <code>proc</code>
134 cmark-py --common-mark <<'EOF'
135- <span /> ... More `proc` features
136- <span />
137 More `proc` features
138- <span /> <!-- why does this fix it? -->
139 More `proc` features
140EOF
141
142 # This has &amp; in an attr value, which our HTML lexer needs to handle
143 cmark-py --common-mark <<'EOF'
144from [ampersand][]
145
146[ampersand]: http://google.com/?q=foo&z=z
147EOF
148
149 # Only &nbsp; is standard
150 cmark-py --common-mark <<'EOF'
151- tr
152 - &nbsp; -
153 - &sp; -
154 - &zwsp; -
155EOF
156
157 # BUG: parse error because backticks span a line
158
159 return
160 cmark-py <<'EOF'
1611. The Markdown translator produces a `<table> <ul> <li> ... </li> </ul>
162 </table>` structure.
163EOF
164}
165
166demo-htm8() {
167 ### Cases that came from developing HTM8
168
169 export PYTHONPATH=.
170
171 cmark-py --common-mark <<'EOF'
172[bash]($xref:bash)
173
174[other][]
175
176[other]: $xref
177
178EOF
179}
180
181demo-quarto() {
182 ### Cases that came from developing HTM8
183
184 export PYTHONPATH=.
185
186 # Standard Markdown
187 cmark-py --common-mark <<'EOF'
188
189Hello
190
191 code
192 block
193
194Python:
195
196```python
197print("hi")
198```
199EOF
200
201 # Quarto extensions
202 # Turns out as class="language-{python} which isn't ideal
203 cmark-py --common-mark <<'EOF'
204
205Executable Python
206
207```{python}
208print("hi")
209```
210
211With attributes
212
213```{python}
214#| attr: value
215#| fig-cap: "A line plot"
216print("hi")
217```
218EOF
219
220 # Another syntax I saw
221 # This appears to be R Markdown, which is an older syntax that Quarto
222 # doesn't use
223 # But it accepts
224 #
225 # Hm cmark omits everything after the space
226
227 cmark-py --common-mark <<'EOF'
228
229Executable Python
230
231```{r setup, include=FALSE}
232print("hi")
233```
234EOF
235}
236
237"$@"