Skip to content

Commit 10fb425

Browse files
committed
Update parse-anchor to include Chap/Section
Detect # vs ## and use the list (:ANCHOR (:C "# Ch Title")) or (:ANCHOR (:S "## Section Title")) depending on which.
1 parent a6605c7 commit 10fb425

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

dev.lit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ Here's a ref. @{foobaz} What's it do?
2323
--- foobaz
2424
(format nil (* 2 2))
2525
---
26+
27+
# Section 2

parse.lisp

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,47 @@
3030
:format-control "unknown modifier ~s"
3131
:format-arguments x))))
3232

33+
(comment
34+
(ppcre:parse-string
35+
"@\{(#{1,2}\\s.*?)\}")
36+
; => (:SEQUENCE "@{"
37+
; (:REGISTER
38+
; (:SEQUENCE (:GREEDY-REPETITION 1 2 #\#) #\s
39+
; (:NON-GREEDY-REPETITION 0 NIL :EVERYTHING)))
40+
; #\})
41+
)
42+
3343
(defparameter *anchor-pattern*
34-
(ppcre:create-scanner '(:SEQUENCE "@{" (:GREEDY-REPETITION 1 2 #\#)
35-
(:GREEDY-REPETITION 1 NIL :WHITESPACE-CHAR-CLASS)
36-
(:REGISTER (:NON-GREEDY-REPETITION 0 NIL :EVERYTHING)) #\})))
44+
(ppcre:create-scanner '(:SEQUENCE "@{"
45+
(:REGISTER
46+
(:SEQUENCE (:GREEDY-REPETITION 1 2 #\#) :WHITESPACE-CHAR-CLASS
47+
(:NON-GREEDY-REPETITION 0 NIL :EVERYTHING)))
48+
#\})
49+
))
3750

3851
(defun parse-anchor (line)
3952
(let ((parts (ppcre:split *anchor-pattern* line :with-registers-p t)))
4053
(mapcar-indexed (lambda (string i)
4154
(if (evenp i)
42-
(ppcre:regex-replace-all "@@({[^}]+})" string "@\\1")
43-
(list :ANCHOR string)))
55+
;; Not sure what to do write here. Should `parse-anchor' have
56+
;; the duplicated escape logic?
57+
;; I'm leaning towards duplicate the same escape logic from `parse-ref' to here.
58+
(progn
59+
string
60+
;; or...
61+
(ppcre:regex-replace-all "@@({[^}]+})" string "@\\1"))
62+
(list :ANCHOR (if (eql (char string 1) #\#)
63+
(list :S (ppcre:regex-replace "##\\s+" string ""))
64+
(list :C (ppcre:regex-replace "#\\s+" string ""))))))
4465
parts)))
4566

67+
4668
(comment
47-
(let ((line "Foobar @{# Baz} @{buz} @@{fizz}"))
69+
;; How should escaping work if we go this route of parse-anchor+parse-ref and parse-repeatedly?
70+
(let ((line "Foobar @{# Baz} @{## Biz} @{buz} @@{fizz}"))
4871
(parse-anchor line))
49-
; => ("Foobar " (:ANCHOR "Baz") " @{buz} @{fizz}")
72+
; => ("Foobar " (:ANCHOR (:C "Baz")) " " (:ANCHOR (:S "Biz")) " @{buz} @@{fizz}")
73+
; => ("Foobar " (:ANCHOR (:C "Baz")) " " (:ANCHOR (:S "Biz")) " @{buz} @{fizz}")
5074
)
5175

5276
(defparameter *ref-pattern*
@@ -135,11 +159,18 @@
135159
Line starts off as a string. After the first parse, it will be a list of regular text and parsed segments.
136160
137161
Example:
138-
\"Some text @{some ref}\"
162+
\"Some @{# some chapter} text @{some ref}\"
139163
will turn into
140-
(\"Some text\" (:INCLUDE \"some ref\")).
164+
(\"Some \" (:ANCHOR \"#some chapter\") \"text \" (:INCLUDE \"some ref\")).
141165
142-
The subsequent parsers will be mapped over the result of the first parse."
166+
The subsequent parsers will be mapped over the result of the first parse.
167+
168+
NOTE:
169+
There's at least one issue with this.
170+
`parse-refs' treats the double `@@' as an escape sequence.
171+
Instead of turning `@@{# foo}' into `@<a href='# foo'></a>' it turns it into `@{# foo}'.
172+
So if we first `parse-refs' and turn `@@{# foo}' into `@{# foo}' and then run `parse-anchors' after that
173+
then we're bypassing our escape mechanism."
143174
(cond
144175
((null line) nil)
145176
((null parsers) line)
@@ -188,25 +219,24 @@ The subsequent parsers will be mapped over the result of the first parse."
188219
line)))
189220

190221
(comment
222+
;; Testing out the `parse-repeatedly' behavior.
223+
191224
(parse-prose-line "\\n")
192225
; => ("\\n")
193226
(parse-prose-line "")
194227
; => NIL
195228
(parse-prose-line "Foobar @{# Baz} \\begin{math}n + m\\end{math} buz @{fizz}")
196-
; => ("Foobar " (:ANCHOR "Baz") " " (:MATH "n + m") " buz " (:INCLUDE "fizz"))
229+
; => ("Foobar " (:ANCHOR (:C "Baz")) " " (:MATH "n + m") " buz " (:INCLUDE "fizz"))
197230
(parse-prose-line "Foobar @{fizz} \\begin{math}n + m\\end{math} buz @{# Baz}")
198-
; => ("Foobar " (:INCLUDE "fizz") " " (:MATH "n + m") " buz " (:ANCHOR "Baz"))
231+
; => ("Foobar " (:INCLUDE "fizz") " " (:MATH "n + m") " buz " (:ANCHOR (:C "Baz")))
199232
(parse-prose-line "# Some heading @{with a ref}")
200233
; => ((:C "Some heading @{with a ref}"))
201234
(mapcar #'parse-prose-line
202235
'("# Foobar"
203236
"@{bazz}"
204237
""
205238
"@{# Foobar}"))
206-
; => (((:C "Foobar")) ("" (:INCLUDE "bazz")) NIL ("@{# Foobar}")) <- original
207-
; => (((:C "Foobar")) ((:INCLUDE "bazz")) NIL ("" (:ANCHOR "Foobar"))) <- new
208-
; Not sure why this (:INCLUDE...) is different between the two.
209-
; I think it will still work. And everything else looks the same.
239+
; => (((:C "Foobar")) ((:INCLUDE "bazz")) NIL ("" (:ANCHOR (:C "Foobar"))))
210240
)
211241

212242
(defparameter *block-start-pattern*
@@ -263,6 +293,15 @@ The subsequent parsers will be mapped over the result of the first parse."
263293
(textblock-lines (textblockdef-block def)))
264294
(go TEXT)))
265295

296+
(comment
297+
;; Just want to get a feel for what the def-table looks like
298+
(let* ((file-defs (parse-lit-files '("dev.lit" "scratch.lit")))
299+
(weaver (make-weaver-default file-defs)))
300+
(let ((defs (weaver-def-table weaver)))
301+
(maphash (lambda (k v)
302+
(format t "~a ~a~%" k v))
303+
defs)))
304+
)
266305

267306
(defparameter *math-block-pattern*
268307
(ppcre:create-scanner

weave.lisp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
(comment
3939
(let* ((file-defs (parse-lit-files '("dev.lit" "scratch.lit")))
4040
(weaver (make-weaver-default file-defs)))
41-
(weaver-toc weaver))
41+
weaver)
42+
4243
; => ((:FILE "dev.lit" (:C "My test lit file" (:S "Foobar") (:S "Foobazs")))
4344
; (:FILE "scratch.lit" (:C "My scratch lit file" (:S "Scratch"))))
4445
)

0 commit comments

Comments
 (0)