1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
# -*- tcl -*-
##
# Support for markdown, overrides parts of coore text.
#
# Copyright (c) 2019 Andreas Kupries <andreas_kupries@sourceforge.net>
# Freely redistributable.
##
# # ## ### ##### ########
# # ## ### ##### ########
## `markdown` formatting
proc SectTitle {lb title} {
upvar 1 $lb lines
lappend lines "[Hash] $title"
return
}
proc SubsectTitle {lb title} {
upvar 1 $lb lines
lappend lines "[Hash][Hash] $title"
return
}
proc Sub3Title {lb title} {
upvar 1 $lb lines
lappend lines "[Hash][Hash][Hash] $title"
return
}
proc Sub4Title {lb title} {
upvar 1 $lb lines
lappend lines "[Hash][Hash][Hash][Hash] $title"
return
}
proc _Strong {text} { return [Undr][Undr]${text}[Undr][Undr] }
proc _Em {text} { return [Star]${text}[Star] }
##
# # ## ### ##### ########
##
set __comments 0
proc MDCInit {} { global __comments ; set __comments 0 }
proc MDComment {text} {
global __comments
Text "\n[OBrk]//[format %09d [incr __comments]][CBrk]: [Hash] [OPar]$text[CPar]"
}
proc MDCDone {} {
TextTrimLeadingSpace
CloseParagraph [Verbatim]
}
##
# # ## ### ##### ########
##
proc MakeLink {l t} { ALink $t $l } ;# - xref - todo: consolidate
proc ALink {dst label} { return "[OBrk]$label[CBrk][OPar]$dst[CPar]" }
proc SetAnchor {text {name {}}} {
if {$name == {}} { set name [Anchor $text] }
return "<a name='$name'></a>$text"
}
proc Anchor {text} {
global kwid
if {[info exists kwid($text)]} {
return "$kwid($text)"
}
return [A $text]
}
proc A {text} {
set anchor [regsub -all {[^a-zA-Z0-9]} [string tolower $text] {_}]
set anchor [regsub -all {__+} $anchor [Undr]]
return $anchor
}
# Generate special code sequences for markdown command characters. At
# the end of the render the command sequences are converted to regular
# final form whereas all unprotected special characters are quoted.
proc Back {} { return "\1\\" }
proc Tick {} { return "\1`" }
proc Star {} { return "\1*" }
proc Undr {} { return "\1_" }
proc Hash {} { return "\1#" }
proc Plus {} { return "\1+" }
proc Dash {} { return "\1-" }
proc Dot {} { return "\1." }
proc Bang {} { return "\1!" }
proc OBra {} { return "\1\{" }
proc CBra {} { return "\1\}" }
proc OBrk {} { return "\1\[" }
proc CBrk {} { return "\1\]" }
proc OPar {} { return "\1(" }
proc CPar {} { return "\1)" }
proc VBar {} { return "\1|" }
proc LB {} { return "\1\n" }
proc LB. {} { return "\1" }
proc c_copyrightsymbol {} {return "©"}
# Modified bulleting
DIB [list [Dash] [Star] [Plus]]
DEB [list "1[Dot]" "1[CPar]"]
proc Unmark {x} {
lappend map "\1\n" " \n"
# Marked special characters are commands. Convert into regular
# form. Unmarked special characters need quoting.
lappend map \1\\ \\
lappend map \1` `
lappend map \1* *
lappend map \1_ _
lappend map \1# "#"
lappend map \1+ +
lappend map \1- -
lappend map \1. .
lappend map \1! !
lappend map \1\{ \{
lappend map \1\} \}
lappend map \1\[ \[
lappend map \1\] \]
lappend map \1( (
lappend map \1) )
lappend map \1| |
lappend map \\ \\\\
lappend map ` \\`
lappend map * \\*
lappend map _ \\_
lappend map "#" "\\#"
lappend map + \\+
lappend map - \\-
lappend map . \\.
lappend map ! \\!
lappend map \{ \\\{
lappend map \} \\\}
lappend map \[ \\\[
lappend map \] \\\]
lappend map ( \\(
lappend map ) \\)
lappend map | "|"
#puts_stderr ZZZ(($x))
set x [string map $map $x]
#puts_stderr ZZZ<<$x>>
set x
}
# Invert handling of special characters for text specified by the
# user, i.e. engine parameters of some kind.
#
# Quoted special characters are unquoted, and unquoted special
# characters get marked/protected.
proc Mark {x} {
# Dequote non-special specials.
lappend map \\\\ \\
lappend map \\` `
lappend map \\* *
lappend map \\_ _
lappend map "\\#" "#"
lappend map \\+ +
lappend map \\- -
lappend map \\. .
lappend map \\! !
lappend map \\\{ \{
lappend map \\\} \}
lappend map \\\[ \[
lappend map \\\] \]
lappend map \\( (
lappend map \\) )
lappend map "|" |
# Protect special specials
lappend map \\ \1\\
lappend map ` \1`
lappend map * \1*
lappend map _ \1_
lappend map "#" \1#
lappend map + \1+
lappend map - \1-
lappend map . \1.
lappend map ! \1!
lappend map \{ \1\{
lappend map \} \1\}
lappend map \[ \1\[
lappend map \] \1\]
lappend map ( \1(
lappend map ) \1)
lappend map | \1|
#puts_stderr ZZZ(($x))
set x [string map $map $x]
#puts_stderr ZZZ<<$x>>
set x
}
rename PostProcess PostProcessT
proc PostProcess {text} { Unmark [PostProcessT $text] }
##
# # ## ### ##### ########
return
|