literate: restructure all 19 org files with per-function blocks and prose
Every function, defclass, defstruct, defgeneric, defmethod, defmacro, defvar, and defparameter in every org file now has its own #+BEGIN_SRC block with literate prose above it explaining the design reasoning. Block counts before → after: package.org: 1 → 7 container-package.org: 1 → 1 (prose expanded) dirty.org: 4 → 6 render.org: 10 → 25 theme.org: 6 → 19 box-renderable.org: 9 → 29 scrollbox.org: 8 → 26 tabbar.org: 5 → 10 backend-protocol.org: 8 → 66 modern-backend.org: 17 → 53 detection.org: 4 → 6 layout-engine.org: 9 → 36 framebuffer.org: 8 → 37 markdown-renderer.org:13 → 38 dialog.org: 17 → 23 (merged dual structure) mouse.org: 4 → 25 select.org: 12 → 30 slot.org: 4 → 12 text-input.org: 11 → 53 Total: ~153 blocks → ~502 blocks Bugs fixed during restructuring: - render.org: stray π character typo (backenπd → backend) - modern-backend.org: sgr-attr missing closing paren + #+END_SRC - detection.org: invalid #\Esc character reference - select.org: extra closing paren in select-visible-options All 13 test suites pass at 100%.
This commit is contained in:
@@ -25,13 +25,33 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
|
||||
** Main module
|
||||
|
||||
The main module file header includes the package declaration and a
|
||||
comment indicating the file's purpose. This block is the first to
|
||||
target ~markdown.lisp~ and thus overwrites any previous content;
|
||||
all subsequent blocks append.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
;;; markdown.lisp — Markdown + Code + Diff rendering for cl-tty
|
||||
|
||||
(in-package :cl-tty.markdown)
|
||||
#+END_SRC
|
||||
|
||||
;; ─── Node constructors ────────────────────────────────────────────────────────
|
||||
*** Node constructors
|
||||
|
||||
Node constructors provide a uniform way to build the AST for parsed
|
||||
Markdown. Using plists (property lists) with a ~:type~ key gives us
|
||||
flexibility — we can attach arbitrary metadata without a rigid class
|
||||
hierarchy, which keeps the parser simple and the data easy to
|
||||
introspect from the REPL.
|
||||
|
||||
**** make-md-node
|
||||
|
||||
~make-md-node~ is the primary constructor. It accepts a required ~type~
|
||||
symbol and optional keyword arguments for ~children~, ~properties~,
|
||||
~content~, and ~url~. Only non-nil slots are stored, keeping the
|
||||
plist compact.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun make-md-node (type &key children properties content url)
|
||||
(let ((node (list :type type)))
|
||||
(when children (setf (getf node :children) children))
|
||||
@@ -39,10 +59,28 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(when content (setf (getf node :content) content))
|
||||
(when url (setf (getf node :url) url))
|
||||
node))
|
||||
#+END_SRC
|
||||
|
||||
**** md-node-p
|
||||
|
||||
Predicate that checks whether a value is an AST node by verifying it
|
||||
is a list and has a ~:type~ property. This uses plist access which
|
||||
bypasses the need for ~typep~ or class-based dispatch.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun md-node-p (thing)
|
||||
(and (listp thing) (getf thing :type)))
|
||||
#+END_SRC
|
||||
|
||||
**** md-node-text
|
||||
|
||||
~md-node-text~ recursively extracts the plain-text representation of a
|
||||
node tree. The ~:link~ type formats as ~text (url)~; ~:text~ and
|
||||
~:inline-code~ return their content directly; other container types
|
||||
concatenate their children's text. This is useful for summarisation
|
||||
and testing.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun md-node-text (node)
|
||||
(let ((type (getf node :type)))
|
||||
(cond ((eql type :text) (or (getf node :content) ""))
|
||||
@@ -55,9 +93,21 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(apply #'concatenate 'string
|
||||
(mapcar #'md-node-text (getf node :children))))
|
||||
(t ""))))
|
||||
#+END_SRC
|
||||
|
||||
;; ─── Block-level parser ───────────────────────────────────────────────────────
|
||||
*** Block-level parser
|
||||
|
||||
The block parser splits raw text into lines and classifies each line
|
||||
to determine what kind of block structure it begins. Helper functions
|
||||
keep the main ~parse-blocks~ dispatch manageable.
|
||||
|
||||
**** split-string-into-lines
|
||||
|
||||
Handles ~CRLF~, ~LF~, and missing trailing newline uniformly.
|
||||
Returns a ~vector~ for fast indexed access by line number during
|
||||
parsing. Returns an empty vector for ~nil~ input.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun split-string-into-lines (string)
|
||||
(unless string (return-from split-string-into-lines (coerce nil 'vector)))
|
||||
(let ((result nil) (start 0))
|
||||
@@ -72,6 +122,14 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(coerce (nreverse result) 'vector))))
|
||||
#+END_SRC
|
||||
|
||||
**** classify-line
|
||||
|
||||
The core line classification function. It checks line prefixes in
|
||||
priority order — blank lines, thematic breaks, ATX headings, blockquote
|
||||
markers, unordered/ordered list items, diff headers, diff lines, and
|
||||
fenced code-block starts — and returns a ~(cons type data)~ pair.
|
||||
Everything else is treated as a paragraph continuation line.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun classify-line (line)
|
||||
(cond
|
||||
@@ -122,7 +180,15 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(subseq line fence-len))))
|
||||
(cons :code-start rest))))))
|
||||
(t (cons :paragraph line))))
|
||||
#+END_SRC
|
||||
|
||||
**** find-closing-marker
|
||||
|
||||
Scans for a literal marker string starting from position ~start~,
|
||||
escaping backslash-escaped markers. This is shared by inline
|
||||
emphasis, code span, and link parsing. Returns the position or ~nil~.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun find-closing-marker (text start marker)
|
||||
(let ((marker-len (length marker)) (len (length text)))
|
||||
(loop for j from start to (- len marker-len)
|
||||
@@ -133,6 +199,13 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
finally (return nil))))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-paragraph
|
||||
|
||||
Collects consecutive paragraph lines (lines classified as ~:paragraph~)
|
||||
into a single ~:paragraph~ node. Stops at a blank line or any
|
||||
non-paragraph classification. Lines are joined with spaces before
|
||||
inline parsing.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-paragraph (lines start)
|
||||
(let ((text-parts nil) (i start))
|
||||
@@ -152,7 +225,15 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
do (unless first (write-char #\Space s))
|
||||
(princ part s)))))
|
||||
i)))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-blockquote
|
||||
|
||||
Like ~parse-paragraph~ but collects ~:blockquote~ lines and strips the
|
||||
leading ~>~ marker. The collected text is then inline-parsed to
|
||||
support bold, italic, code, and links inside quotes.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-blockquote (lines start)
|
||||
(let ((text-parts nil) (i start))
|
||||
(loop while (< i (length lines))
|
||||
@@ -173,6 +254,14 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
i)))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-list
|
||||
|
||||
Handles both unordered (~:list-item~) and ordered (~:ordered-item~)
|
||||
list items. Adjacent blank lines between items are allowed (creating
|
||||
loose lists), but a blank line followed by a non-list line terminates
|
||||
the list. Returns multiple nodes because each top-level list item
|
||||
becomes its own ~:list-item~ or ~:ordered-item~ node.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-list (lines start)
|
||||
(let ((items nil) (i start))
|
||||
@@ -200,6 +289,14 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(values (nreverse nodes) i))))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-code-block
|
||||
|
||||
Parses a fenced code block starting at ~start~. The fence character
|
||||
and length are detected from the opening line; the closing fence must
|
||||
match in character and be at least as long. The language (if any) is
|
||||
taken from the info string on the opening fence. Produces a single
|
||||
~:code-block~ node.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-code-block (lines start lang)
|
||||
(let ((code-lines nil)
|
||||
@@ -227,7 +324,16 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
for first = t then nil
|
||||
do (unless first (terpri s)) (princ cl s))))
|
||||
i)))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-diff-block
|
||||
|
||||
Collects consecutive diff lines (~:diff-header~, ~:diff-line~) into a
|
||||
single ~:diff-block~ node. The raw lines are preserved in a ~:lines~
|
||||
property for coloured rendering later. Diff blocks are delimited by
|
||||
blank lines.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-diff-block (lines start)
|
||||
(let ((diff-lines nil) (i start))
|
||||
(loop while (< i (length lines))
|
||||
@@ -249,6 +355,14 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
i))))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-blocks
|
||||
|
||||
Top-level block parser. Dispatches on the ~classify-line~ result to
|
||||
call the appropriate sub-parser, accumulating nodes into a list.
|
||||
Handles blank lines, thematic breaks, headings, paragraphs,
|
||||
blockquotes, lists, code blocks, and diff blocks. Returns ~nil~ for
|
||||
~nil~ input.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-blocks (text)
|
||||
(unless text (return-from parse-blocks nil))
|
||||
@@ -289,9 +403,20 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(nreverse nodes)))
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
;; ─── Inline parser ────────────────────────────────────────────────────────────
|
||||
*** Inline parser
|
||||
|
||||
The inline parser handles character-level formatting inside block
|
||||
content: emphasis, code spans, and links.
|
||||
|
||||
**** parse-inline
|
||||
|
||||
Main inline dispatcher. Walks the text character by character.
|
||||
~*~ triggers star emphasis; ~_~ triggers underscore emphasis; ~`~
|
||||
triggers inline code; ~[~ triggers links; everything else is
|
||||
accumulated as plain ~:text~ nodes. Consecutive plain text is merged
|
||||
into single nodes for efficiency.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-inline (text)
|
||||
(unless (and text (> (length text) 0)) (return-from parse-inline nil))
|
||||
(let ((nodes nil) (i 0) (len (length text)))
|
||||
@@ -327,7 +452,17 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(incf i)))))
|
||||
(push (make-md-node :text :content (subseq text start i)) nodes))))))
|
||||
(nreverse nodes)))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-star-emphasis
|
||||
|
||||
Handles ~*italic*~ and ~**bold**~ using star markers. A double star
|
||||
is tried first; if the closing ~**~ is found it produces a ~:bold~
|
||||
node, otherwise it falls back to single-star ~:italic~. If neither
|
||||
closes, returns ~nil~ to let the caller treat the character as literal
|
||||
text.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-star-emphasis (text i len)
|
||||
(when (>= i len) (return-from parse-star-emphasis (values nil i)))
|
||||
(if (and (< (1+ i) len) (char= (char text (1+ i)) #\*))
|
||||
@@ -341,7 +476,17 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(values (make-md-node :italic :children (parse-inline (subseq text (1+ i) close)))
|
||||
(1+ close))
|
||||
(values nil i)))))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-underscore-emphasis
|
||||
|
||||
Handles ~_italic_~ and ~__bold__~ using underscore markers.
|
||||
Underscore emphasis is more restrictive than star emphasis: it only
|
||||
opens after whitespace or at the start of text, and single-underscore
|
||||
italic only closes before whitespace or punctuation. This avoids false
|
||||
positives in identifiers like ~foo_bar~.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-underscore-emphasis (text i len)
|
||||
(when (>= i len) (return-from parse-underscore-emphasis (values nil i)))
|
||||
(when (and (> i 0) (not (find (char text (1- i)) " \t\n\r")))
|
||||
@@ -359,7 +504,15 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(values (make-md-node :italic :children (parse-inline (subseq text (1+ i) close)))
|
||||
(1+ close))
|
||||
(values nil i)))))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-inline-code
|
||||
|
||||
Parses backtick-delimited inline code spans. Supports up to three
|
||||
backticks as delimiters (so single backticks inside double-backtick
|
||||
spans work). The matched pair's backtick count must be equal.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-inline-code (text i len)
|
||||
(when (or (>= i len) (not (char= (char text i) #\`)))
|
||||
(return-from parse-inline-code (values nil i)))
|
||||
@@ -372,7 +525,16 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
:content (subseq text (+ i bt-count) close))
|
||||
(+ close bt-count))
|
||||
(values nil i)))))
|
||||
#+END_SRC
|
||||
|
||||
**** parse-link
|
||||
|
||||
Parses Markdown links in the form ~[text](url)~. Uses nested bracket
|
||||
matching via ~find-closing-marker~. The text portion is inline-parsed
|
||||
to support formatting inside link text. Returns ~nil~ if the syntax
|
||||
is incomplete, letting the caller render the ~[~ as literal text.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun parse-link (text i len)
|
||||
(when (or (>= i len) (not (char= (char text i) #\[)))
|
||||
(return-from parse-link (values nil i)))
|
||||
@@ -389,9 +551,24 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(1+ close-paren)))))
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
;; ─── Syntax highlighting ──────────────────────────────────────────────────────
|
||||
*** Syntax highlighting
|
||||
|
||||
Syntax highlighting tokenises source code into (token . category) pairs
|
||||
that the renderer colours with ANSI escape codes. Each supported
|
||||
language has a definition of comment, string, keyword, and builtin
|
||||
patterns.
|
||||
|
||||
**** get-highlighter
|
||||
|
||||
Returns a plist of highlighting rules for a given language name.
|
||||
The rules define ~:comment~, ~:string~, ~:keyword~, and ~:builtin~
|
||||
patterns. Supported languages: lisp, common-lisp, python,
|
||||
javascript, bash, shell. Unknown languages return ~nil~, which tells
|
||||
the caller to fall back to plain rendering. The assoc list uses
|
||||
~string=~ for matching on the language tag, and each entry uses a
|
||||
dotted-pair format ~(\"language\" . plist)~.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun get-highlighter (lang)
|
||||
(cdr (assoc lang
|
||||
'(("lisp" . (:comment (";" "#|" ";;") :string ("\"")
|
||||
@@ -479,6 +656,15 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
:test #'string=)))
|
||||
#+END_SRC
|
||||
|
||||
**** tokenize-line
|
||||
|
||||
Tokenises a single line of source code into ~(token . category)~
|
||||
pairs. Categories are ~:plain~, ~:comment~, ~:string~, ~:number~,
|
||||
~:keyword~, ~:builtin~, and ~:function~. The highlighter plist
|
||||
provides the patterns for comment delimiters, string delimiters,
|
||||
keywords, and builtins. Words immediately followed by ~(~ are
|
||||
classified as ~:function~ calls.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun tokenize-line (line highlighter)
|
||||
(let ((tokens nil) (i 0) (len (length line))
|
||||
@@ -546,7 +732,17 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(push (cons token :plain) tokens)))))))
|
||||
(t (push (cons (string c) :plain) tokens) (incf i)))))
|
||||
(nreverse tokens)))
|
||||
#+END_SRC
|
||||
|
||||
**** highlight-code
|
||||
|
||||
Applies syntax highlighting to a whole code string. Splits the code
|
||||
into lines, tokenises each line with the language's highlighter, and
|
||||
returns a flat list of ~(token . category)~ pairs with newline
|
||||
separators between lines. Returns ~nil~ for empty input or a single
|
||||
~:plain~ pair if no highlighter is found for the language.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun highlight-code (code language)
|
||||
(unless code (return-from highlight-code nil))
|
||||
(let ((highlighter (get-highlighter (and language (string-downcase language)))))
|
||||
@@ -558,25 +754,59 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(when tokens (push (cons (string #\Newline) :plain) tokens))
|
||||
(setf tokens (nconc (nreverse line-tokens) tokens)))))
|
||||
(nreverse tokens))))
|
||||
#+END_SRC
|
||||
|
||||
**** apply-highlight-token
|
||||
|
||||
Wraps a single token in an ANSI escape code based on its highlight
|
||||
category. Keywords get colour 33 (yellow), builtins 36 (cyan),
|
||||
functions 34 (blue), comments 2 (dim), strings 32 (green), numbers
|
||||
35 (magenta). Unrecognised categories render as plain text.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun apply-highlight-token (token category)
|
||||
(let ((code (case category
|
||||
(:keyword "33") (:builtin "36")
|
||||
(:function "34") (:comment "2") (:string "32") (:number "35")
|
||||
(t nil))))
|
||||
(if code (format nil "~c[~am~a~c[0m" #\Esc code token #\Esc) token)))
|
||||
#+END_SRC
|
||||
|
||||
**** apply-highlight-style
|
||||
|
||||
Coerces an adjustable character vector (accumulated during line
|
||||
rendering) back into a string. This is a thin wrapper that exists
|
||||
for potential future customisation of style application.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun apply-highlight-style (char-vector)
|
||||
(coerce char-vector 'string))
|
||||
#+END_SRC
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
;; ─── Diff rendering ───────────────────────────────────────────────────────────
|
||||
*** Diff rendering
|
||||
|
||||
The diff rendering utilities classify diff lines and produce
|
||||
colourised output.
|
||||
|
||||
**** string-prefix-p
|
||||
|
||||
Utility predicate that checks whether ~string~ starts with ~prefix~.
|
||||
Avoids reimplementing this inline in multiple diff classifiers.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun string-prefix-p (prefix string)
|
||||
(and (>= (length string) (length prefix))
|
||||
(string= prefix (subseq string 0 (length prefix)))))
|
||||
#+END_SRC
|
||||
|
||||
**** classify-diff-line
|
||||
|
||||
Classifies a single diff line into a semantic category: ~:file-header~
|
||||
(for ~+++~ and ~---~ lines), ~:hunk-header~ (for ~@@~ lines), ~:added~
|
||||
(for ~+~ lines), ~:removed~ (for ~-~ lines), or ~:context~ (for
|
||||
everything else). This powers colourised diff rendering.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun classify-diff-line (line)
|
||||
(cond ((string-prefix-p "+++ " line) :file-header)
|
||||
((string-prefix-p "--- " line) :file-header)
|
||||
@@ -584,9 +814,23 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
((string-prefix-p "+" line) :added)
|
||||
((string-prefix-p "-" line) :removed)
|
||||
(t :context)))
|
||||
#+END_SRC
|
||||
|
||||
;; ─── Rendering ────────────────────────────────────────────────────────────────
|
||||
*** Rendering
|
||||
|
||||
The rendering layer converts parsed AST nodes into styled terminal
|
||||
output strings. Each node type has its own renderer, and
|
||||
~render-md-node~ dispatches to the correct one.
|
||||
|
||||
**** apply-style
|
||||
|
||||
Wraps ~text~ in ANSI escape codes for a given ~style~ keyword or
|
||||
string. Supports both keyword (e.g. ~:bold~) and string (e.g.
|
||||
~\"bold\"~) style designators for flexibility. Common styles include
|
||||
bold, italic, dim, code, link, underline, and the full set of 16
|
||||
terminal colours. Unrecognised styles return the text unchanged.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun apply-style (style text)
|
||||
(let ((code (cond
|
||||
((eql style :bold) "1") ((eql style :italic) "3")
|
||||
@@ -619,6 +863,13 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(if code (format nil "~c[~am~a~c[0m" #\Esc code text #\Esc) text)))
|
||||
#+END_SRC
|
||||
|
||||
**** render-inline
|
||||
|
||||
Renders a list of inline child nodes into a single string. Handles
|
||||
~:text~ (plain), ~:bold~, ~:italic~, ~:inline-code~, and ~:link~
|
||||
types. Links render the text styled as link followed by the URL in
|
||||
parentheses styled as url.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-inline (children)
|
||||
(if (null children) ""
|
||||
@@ -637,7 +888,16 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(princ " " s)
|
||||
(princ (apply-style :url (format nil "(~a)" url)) s))))
|
||||
(t (princ (or (getf child :content) "") s))))))))
|
||||
#+END_SRC
|
||||
|
||||
**** render-heading
|
||||
|
||||
Renders a heading node as a coloured ~# Title~ line. The heading
|
||||
level determines the number of ~#~ characters (capped at 6) and the
|
||||
colour: level 1 uses bright-cyan, level 2 uses bright-yellow, and
|
||||
deeper levels use bright-white.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-heading (node)
|
||||
(let* ((level (or (getf (getf node :properties) :level) 1))
|
||||
(prefix (make-string (min level 6) :initial-element #\#))
|
||||
@@ -645,15 +905,36 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(color (cond ((= level 1) :bright-cyan) ((= level 2) :bright-yellow)
|
||||
(t :bright-white))))
|
||||
(list (apply-style color (concatenate 'string prefix " " text)))))
|
||||
#+END_SRC
|
||||
|
||||
**** render-paragraph
|
||||
|
||||
Renders a paragraph node by inline-rendering its children. The
|
||||
result is a single-element list containing the rendered text.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-paragraph (node)
|
||||
(list (render-inline (getf node :children))))
|
||||
#+END_SRC
|
||||
|
||||
**** render-blockquote
|
||||
|
||||
Renders a blockquote node with a dimmed ~> ~ prefix before the
|
||||
inline-rendered content.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-blockquote (node)
|
||||
(list (apply-style :dim (concatenate 'string "> " (render-inline (getf node :children))))))
|
||||
#+END_SRC
|
||||
|
||||
**** render-code-block
|
||||
|
||||
Renders a fenced code block. If the block has a language tag and the
|
||||
highlighter supports it, the code is syntax-highlighted with ANSI
|
||||
colours. Otherwise it is rendered in plain ~:code~ style. A dimmed
|
||||
language header line is shown when a language is present.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-code-block (node)
|
||||
(let* ((language (or (getf (getf node :properties) :language) ""))
|
||||
(content (or (getf node :content) ""))
|
||||
@@ -681,7 +962,16 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(loop for line = (read-line s nil nil) while line
|
||||
do (push (apply-style :code line) lines))))
|
||||
(nreverse lines)))
|
||||
#+END_SRC
|
||||
|
||||
**** render-diff-block
|
||||
|
||||
Renders a diff block by classifying each line and applying
|
||||
colour: added lines in green (32), removed in red (31), hunk headers
|
||||
in cyan (36), file headers in bold-cyan (1;36), and context lines
|
||||
unstyled.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-diff-block (node)
|
||||
(let* ((lines (getf (getf node :properties) :lines)) (result nil))
|
||||
(dolist (line (or lines
|
||||
@@ -696,16 +986,38 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(push (format nil "~c[~am~a~c[0m" #\Esc color line #\Esc) result)
|
||||
(push line result))))
|
||||
(nreverse result)))
|
||||
#+END_SRC
|
||||
|
||||
**** render-thematic-break
|
||||
|
||||
Renders a thematic break as a dimmed horizontal rule using
|
||||
Unicode box-drawing characters.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-thematic-break (node)
|
||||
(declare (ignore node))
|
||||
(list (apply-style :dim "──────────────────────────────────────────────")))
|
||||
#+END_SRC
|
||||
|
||||
**** render-list-item
|
||||
|
||||
Renders a list item node. Ordered items get ~ 1.~ prefix,
|
||||
unordered items get ~ * ~ prefix. The content is inline-rendered.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-list-item (node)
|
||||
(list (concatenate 'string
|
||||
(if (eql (getf node :type) :ordered-item) " 1." " * ")
|
||||
(render-inline (getf node :children)))))
|
||||
#+END_SRC
|
||||
|
||||
**** render-md-node
|
||||
|
||||
Dispatcher function that routes a single AST node to the correct
|
||||
renderer based on its ~:type~. Each type-specific renderer returns a
|
||||
list of strings (multiple lines), which ~render-md~ concatenates.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-md-node (node)
|
||||
(let ((type (getf node :type)))
|
||||
(case type
|
||||
@@ -718,12 +1030,28 @@ and diff rendering. Self-contained in ~cl-tty.markdown~ package.
|
||||
(:list-item (render-list-item node))
|
||||
(:ordered-item (render-list-item node))
|
||||
(t (list "")))))
|
||||
#+END_SRC
|
||||
|
||||
**** render-md
|
||||
|
||||
Renders a list of AST nodes (the output of ~parse-blocks~) into a
|
||||
flat list of output lines by calling ~render-md-node~ on each node
|
||||
and concatenating the results.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-md (nodes)
|
||||
(let ((lines nil))
|
||||
(dolist (node nodes) (setf lines (nconc lines (render-md-node node))))
|
||||
lines))
|
||||
#+END_SRC
|
||||
|
||||
**** render-markdown
|
||||
|
||||
Top-level convenience function that parses a Markdown string and
|
||||
renders it to a single output string with newline-separated lines.
|
||||
Returns an empty string for ~nil~ input.
|
||||
|
||||
#+BEGIN_SRC lisp :tangle ../src/components/markdown.lisp
|
||||
(defun render-markdown (text)
|
||||
(unless text (return-from render-markdown ""))
|
||||
(let ((nodes (parse-blocks text)) (parts nil))
|
||||
|
||||
Reference in New Issue
Block a user