File: idx.markdown

package info (click to toggle)
tcllib 1.20%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 68,064 kB
  • sloc: tcl: 216,842; ansic: 14,250; sh: 2,846; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 107; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (219 lines) | stat: -rw-r--r-- 4,764 bytes parent folder | download | duplicates (4)
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
217
218
219
# -*- tcl -*-
# Engine to convert a docidx document into markdown text.
#
# Copyright (c) 2019 Andreas Kupries <andreas_kupries@sourceforge.net>
# Freely redistributable.
#
######################################################################

dt_source _idx_common.tcl
dt_source _text.tcl
dt_source _markdown.tcl

proc c_copyrightsymbol {} {return "(c)"}

######################################################################
# Conversion specification.
# One-pass processing.

rename idx_postprocess {}
rename text_postprocess idx_postprocess
proc   fmt_plain_text {text} {return {}}

################################################################
## Backend for plain text markup

proc fmt_index_begin {label title} {
    MDCInit
    if {($label != {}) && ($title != {})} {
	set title "$label -- $title"
    } elseif {$label != {}} {
	set title $label
    } elseif {$title != {}} {
	 # title is set
    }

    TextInitialize

    MDComment "Index [Provenance]"
    MDCDone

    SectTitle hdr $title
    Text [Compose hdr]
    CloseParagraph [Verbatim]
    return
}

proc fmt_index_end {} {
    LoadKwid
    NavBar
    Keys
    return
    
    set rmargin [RMargin $max]

    incr max
    set blank [Blank $max] ;# indent

    foreach key [lsort [array names map]] {
	set keys [join $map($key) ", "]
	Text [InFlow $keys $rmargin [ReHead $blank $key] $blank]
	CloseParagraph [Verbatim]
    }
    return
}

proc fmt_key {text} {
    global key lk ch
    set lk $text
    set key($lk) {}
    set ch([F $lk]) .
    return
}

proc fmt_manpage {f l} {Ref [dt_fmap $f] $l}
proc fmt_url     {u l} {Ref $u           $l}
proc fmt_comment {text}       {return}

# ### ### ### ######### ######### #########

proc NavBar {} {
    global ch dot
    if {![array size ch]} return
    
    set nav {}
    foreach c [lsort -dict [array names ch]] {
	set ref c[F $c]
	set ch($c) $ref
	lappend nav [ALink [Hash]$ref $c]
    }

    Separator
    
    Text [join $nav $dot]
    CloseParagraph [Verbatim]

    Separator
    return
}

proc Keys {} {
    global key
    set lc {}
    set kwlist {}

    # For a good display we sort keywords in dictionary order.
    # We ignore their leading non-alphanumeric characters.
    set kwlist {}
    foreach kw [array names key] {
       set kwx [string trim [regsub -all {^[^a-zA-Z0-9]+} $kw {}]]
       lappend kwlist [list $kwx $kw]
    }
    foreach item [lsort -index 0 -dict $kwlist] {
       foreach {_ k} $item break
	set c [F $k]
	if {$lc != $c} {
	    CloseParagraph [Verbatim]
	    Section $c ; set lc $c
	}
	BeginKey   $k
	References $k
	EndKey
    }

    CloseParagraph [Verbatim]
    return

}

proc Section {c} {
    global ch
    Text "[Hash][Hash][Hash][Hash] [SetAnchor "Keywords: $c" $ch($c)]"
    CloseParagraph [Verbatim]

    Text "[VBar][VBar][VBar]\n"
    Text "[VBar][Dash][Dash][Dash][VBar][Dash][Dash][Dash][VBar]\n"
    return
}

proc BeginKey {k} {
    Text "[VBar][SetAnchor $k][VBar]"
}

proc References {k} {
    global key dot
    set refs {}
    foreach {ref label} $key($k) {
	lappend refs [ALink $ref $label]
    }
    Text [join $refs $dot]
    return
}

proc EndKey {} {
    Text "[VBar]\n"
}

proc Separator {} {
    Text [Dash][Dash][Dash][Dash]
    CloseParagraph [Verbatim]
}

# ### ### ### ######### ######### #########
## Engine state

proc LoadKwid {} {
    global kwid
    # Engine parameter - load predefined keyword anchors.
    set             ki [Get kwid]
    if {![llength  $ki]} return
    array set kwid $ki
    return
}

proc Ref {r l} {
    global  key  lk
    lappend key($lk) $r $l
    return
}

proc F {text} {
    # Keep only alphanumeric, take first, uppercase
    # Returns nothing if input has no alphanumeric characters.
    return [string toupper [string index [regsub -all {[^a-zA-Z0-9]} $text {}] 0]]
}

# key  : string -> dict(ref -> label) "key formatting"
# ch   : string -> '.'                "key starting characters"
# lk   : string                       "last key"
# kwid : string -> ...
# even : bool

global key  ; array set key  {}
global ch   ; array set ch   {}
global lk   ; set       lk   {}
global la   ; set       la   {}
global ti   ; set       ti   {}
global kwid ; array set kwid {}
global dot  ; set dot   " &[Hash]183; "

# ### ### ### ######### ######### #########
## Engine parameters

global    __var
array set __var {
    kwid {}
}
proc Get               {varname}      {global __var ; return $__var($varname)}
proc idx_listvariables {}             {global __var ; return [array names __var]}
proc idx_varset        {varname text} {
    global __var
    if {![info exists __var($varname)]} {
	return -code error "Unknown engine variable \"$varname\""
    }
    set __var($varname) $text
    return
}

##
# ### ### ### ######### ######### #########