It's online now. Also, here's the code:
#!/usr/bin/env tclsh
package require Tcl 8.6
# Link back to table of contents.
set top {<a href="#table_of_contents" style="font-size: small">[top]</a>}
# Process each named file.
foreach file $argv {
# Read Markdown file.
set chan [open $file]
set data [chan read $chan]
chan close $chan
# Check if the Markdown file contains a TOC.
if {[string first <!--TOC--> $data] >= 0} {
# If so, build the TOC to contain all first- and second-level
headings.
# Consider only headings using "#" and "##" marks, not underlines.
set start 0
set toc <!--TOC-->
append toc "\n<a name=\"table_of_contents\"></a>"
append toc " Table of Contents\n" [string repeat = 50]
while {[regexp -indices -line -start $start {^##?[^#].*} $data
match]} {
# Get line from input.
set line [string range $data {*}$match]
# Get heading level.
regexp {^(#*)(.*)} $line _ heading line
# Strip the anchor tag if present.
regsub {^ <a name=".*"></a>} $line {} line
# Strip the link to the TOC if present.
regsub { <a href="#.*".*} $line {} line
# Extract the title.
set title [string trim $line]
# Compute the anchor name and markup.
regsub -all {\W} [string tolower $title] _ name
set anchor "<a name=\"$name\"></a>"
# Build table of contents.
append toc \n
if {$heading eq "##"} {
append toc " "
}
append toc "* \[$title\](#$name)"
# Replace the section header line.
set line "$heading $anchor $title $top"
set data [string replace $data {*}$match $line]
# Update the start index.
set start [expr {[lindex $match 0] + [string length $line]}]
}
append toc \n
# Write Markdown file with the table of contents inserted.
set chan [open $file w]
chan puts -nonewline $chan [regsub -line
{^<!--TOC-->$(?:\n.+$)*\n$}\
$data [string map {& \& \\ \\\\} $toc]]
chan close $chan
}
}
# vim: set sts=4 sw=4 tw=80 et ft=tcl:
On Aug 21, 2017 09:47, "jungle Boogie" <[email protected]> wrote:
> On 20 August 2017 at 10:24, Andy Goth <[email protected]> wrote:
> > On 08/18/17 07:38, Dewey Hylton wrote:
> >>
> >> I predict this to be the best email I receive today.
> >>
> >> My first thought was "This is like paid support!"
> >> My second thought was "Wait ... paid support has *never* been this good
> >> ..."
> >>
> >> Thanks for your work!
> >
> >
> > Since you seem to be really interested, have one more improvement. Now
> > there's no need for an <!--END--> marker. The table of contents extends
> > from <!--TOC--> to the first blank line. Aside from looking cleaner
> > overall, this works around a Markdown rendering oddity which required me
> to
> > put a blank line between the last line of the TOC list and the <!--END-->
> > line.
> >
> >
> > https://chiselapp.com/user/andy/repository/brush/file/doc/toc.tcl
> >
>
>
> Any chance of using something like https://www.pastiebin.com to show the
> code?
> chiselapp.com is offline, and it looks like that's been the case of a
> few days now.
> _______________________________________________
> fossil-users mailing list
> [email protected]
> http://lists.fossil-scm.org:8080/cgi-bin/mailman/listinfo/fossil-users
>
_______________________________________________
fossil-users mailing list
[email protected]
http://lists.fossil-scm.org:8080/cgi-bin/mailman/listinfo/fossil-users