From: Nishi Date: Mon, 30 Sep 2024 05:05:58 +0000 (+0000) Subject: working X-Git-Url: https://git.chaotic.ninja/gitweb/nishi/?a=commitdiff_plain;h=e9a58d237477dc22ec08784387a2e5120b2d462a;p=docgen.git working git-svn-id: file:///raid/svn-personal/docgen/trunk@1 44bc13e5-44cb-984e-b856-1907bf1ada44 --- e9a58d237477dc22ec08784387a2e5120b2d462a diff --git a/README b/README new file mode 100644 index 0000000..bfa9e9b --- /dev/null +++ b/README @@ -0,0 +1 @@ +This is docgen - Document generator diff --git a/docgen b/docgen new file mode 100755 index 0000000..95e4635 --- /dev/null +++ b/docgen @@ -0,0 +1,338 @@ +#!/usr/bin/env tclsh +# $Id$ + +set DOCGEN_VERSION "1.00" +set INPUT "docgen.conf" +foreach arg $argv { + if { [string range "$arg" 0 0] == "-" } { + if { "$arg" == "-V" } { + puts "Docgen version $DOCGEN_VERSION" + exit 0 + } else { + puts "Invalid option: $arg" + exit 1 + } + } else { + set INPUT "$arg" + } +} + +if { ![file exists "$INPUT"] } { + puts "Config $INPUT does not exist" + exit 1 +} + +set input_directory "" +set output_directory "" +set title "" +set icon "" +set footer "" +set links "" + +source "$INPUT" + +if { "$input_directory" == "" } { + puts "Set input_directory" + exit 1 +} +if { "$output_directory" == "" } { + puts "Set output_directory" + exit 1 +} +if { "$title" == "" } { + puts "Set title" + exit 1 +} + +if { "$icon" != "" && ![file exists "$icon"] } { + puts "Icon $icon does not exist" + exit 1 +} + +if { "$links" != "" } { + set links " | $links" +} + +set genre_list {} + +puts "Docgen version $DOCGEN_VERSION" + +proc start_html {fid title toc dots} { + global links + puts $fid "" + puts $fid "" + puts $fid " " + puts $fid " " + puts $fid " $title" + puts $fid " " + puts $fid " " + puts $fid " " + puts $fid "
$title
" + puts $fid " Root$links" + puts $fid "
" + if { "$toc" != "" } { + puts $fid "
" + puts $fid "
" + puts $fid "
" + puts $fid " $toc" + puts $fid "
" + puts $fid "
" + puts $fid "
" + } +} + +proc end_html {fid} { + global footer + puts $fid "
" + puts $fid "
" + if { "$footer" != "" } { + puts $fid "
" + puts $fid " $footer" + } + puts $fid " " + puts $fid "" +} + +proc parse_markdown {path _result} { + global title icon + upvar $_result result + + set result(title) "$title" + set result(body) "" + set result(toc) "" + set result(genre) "" + + set fid [open "$path" "r"] + + set blank 0 + set has_h2 0 + set codeblock "" + set code "" + + while { [gets $fid line] >= 0} { + while 1 { + if { $codeblock != "" } { + if { [regexp -- {```} "$line"] } { + set hl "" + if { "$codeblock" == "plain" } { + set hl [exec -keepnewline -ignorestderr enscript --color=1 -whtml -o - << $code 2>/dev/null] + } else { + set hl [exec -keepnewline -ignorestderr enscript --color=1 -whtml -o - -E$codeblock << $code 2>/dev/null] + } + regexp -- {
.+
} "$hl" line + set result(body) "$result(body)$line" + set codeblock "" + break + } else { + set code "$code$line\n" + } + } elseif { [string length "$line"] == 0 } { + incr blank + if { $blank == 2 } { + set result(body) "$result(body)\n
" + set blank 0 + } + } else { + set blank 1 + set line "[regsub -all {\*\*\*(.+)\*\*\*} "$line" {\1}]" + set line "[regsub -all {\*\*(.+)\*\*} "$line" {\1}]" + set line "[regsub -all {\*(.+)\*} "$line" {\1}]" + while 1 { + if { [regexp -- {```} "$line"] } { + if { $codeblock == "" } { + regexp -- {```([a-zA-Z\-]+)?} "$line" -> codeblock + if { $codeblock == "" } { + set codeblock "plain" + } + set line "[regsub {```([a-zA-Z\-]+)?} "$line" ""]" + set code "" + } + } else { + break + } + } + set line "[regsub -all {\`([^`]+)\`} "$line" {\1}]" + set line "[regsub -all {\!\[([^\]]+)\]\(([^\)]+)\)} "$line" {\1}]" + set line "[regsub -all {\[([^\]]+)\]\(([^\)]+)\)} "$line" {\1}]" + if { [regexp -- {^#+ } "$line" ] } { + regexp -- {^(#+) } "$line" -> hashes + set n [string length "$hashes"] + set link "" + set name "" + + if { $n == 1 } { + if { [regexp -- {^(.+) \{([^\}]+)\}$} "[regsub {^(#+) } "$line" {}]" -> pagetitle genre] } { + set result(genre) "$genre" + } elseif { [regexp -- {^(.+)$} "[regsub {^(#+) } "$line" {}]" -> pagetitle] } { + } + set result(title) "$pagetitle - $title" + set name "$pagetitle" + } else { + if { $has_h2 == 0 } { + set result(body) "$result(body)\n
" + } + set has_h2 1 + set result(toc) "$result(toc)[regsub {^(#+) } "$line" {}]
" + set link "#" + set name [regsub {^(#+) } "$line" {}] + } + set line "$link $name" + } + if { [regexp -- {\\$} "$line" ] } { + set line "[regsub {\\$} "$line" {
}]" + } + set result(body) "$result(body)$line" + } + break + } + } + + if { $has_h2 == 1 } { + set result(body) "$result(body)\n
" + } + + close $fid +} + +proc rescan {path dots} { + global output_directory input_directory icon genre_list + foreach name [glob -tails -nocomplain -directory "$input_directory/$path" *] { + if { [file type "$input_directory/$path/$name"] == "directory" } { + file mkdir "$output_directory/$path/$name" + rescan "$path/$name" "$dots../" + } elseif { "$path" == "" } { + puts "Putting the file on the top-level directory is illegal" + exit 1 + } elseif { "[file extension "$input_directory/$path/$name"]" == ".md" } { + puts "* $path/$name" + array set result {} + parse_markdown "$input_directory/$path/$name" result + + set esc "[regsub -all { } "$result(genre)" _]" + set name "genre_$esc" + global $name + if { [lsearch $genre_list "$esc"] == -1 } { + lappend genre_list "$esc" + } + lappend "$name" "$path" "$result(title)"; + + set outfid [open "$output_directory/$path/index.html" "w"] + if { "$icon" != "" } { + set result(toc) "\"logo\"
$result(toc)" + } + start_html $outfid "$result(title)" "$result(toc)" "$dots" + puts $outfid "$result(body)" + end_html $outfid + close $outfid + } + } +} + +file mkdir "$output_directory" +rescan "" "" + +if { "$icon" != "" } { + file copy -force "$icon" "$output_directory/[file tail "$icon"]" +} + +set fid [open "$output_directory/style.css" "w"] + +set COLOR_PURPLE "#600060" +set COLOR_WHITE "#ffffff" +set COLOR_GRAY "#c0c0c0" +set COLOR_BLUEDARKGRAY "#606080" +set COLOR_DARKGRAY "#808080" +set COLOR_LIGHTGRAY "#f0f0f0" + +set COLOR_BG "$COLOR_WHITE" +set COLOR_TEXT "$COLOR_BLUEDARKGRAY" +set COLOR_VISITED "#a0a0a0" +set COLOR_LINK "#8080f0" + +puts $fid "#title {" +puts $fid " font-size: 30px;" +puts $fid " font-family: sans-serif;" +puts $fid " background-color: $COLOR_TEXT;" +puts $fid " color: $COLOR_BG;" +puts $fid " padding: 5px 0;" +puts $fid " padding-right: 25%;" +puts $fid " text-align: right;" +puts $fid "}" +puts $fid "html {" +puts $fid " color: $COLOR_TEXT;" +puts $fid " font-family: sans-serif;" +puts $fid "}" +puts $fid "a {" +puts $fid " color: $COLOR_LINK;" +puts $fid "}" +puts $fid "a:visited {" +puts $fid " color: $COLOR_VISITED;" +puts $fid "}" +puts $fid "h1 {" +puts $fid " font-size: 30px;" +puts $fid " font-family: sans-serif;" +puts $fid " padding: 5px;" +puts $fid " text-align: left;" +puts $fid " border: solid 1px $COLOR_TEXT;" +puts $fid "}" +puts $fid "#toc {" +puts $fid " float: right;" +puts $fid " width: 13em;" +puts $fid " background-color: $COLOR_LIGHTGRAY;" +puts $fid "}" +puts $fid "pre {" +puts $fid " background-color: $COLOR_LIGHTGRAY;" +puts $fid " padding: 10px;" +puts $fid "}" +puts $fid "img {" +puts $fid " border: none;" +puts $fid "}" +puts $fid "code {" +puts $fid " background-color: $COLOR_LIGHTGRAY;" +puts $fid " padding: 0px 5px;" +puts $fid "}" +puts $fid "#docinside {" +puts $fid " padding-right: 14em;" +puts $fid "}" +puts $fid "#tocinside {" +puts $fid " padding: 5px;" +puts $fid "}" +puts $fid "#shift {" +puts $fid " padding-left: 30px;" +puts $fid "}" +puts $fid "hr {" +puts $fid " border: solid 1px $COLOR_TEXT;" +puts $fid "}" +puts $fid "h2 {" +puts $fid " font-size: 20px;" +puts $fid " font-family: sans-serif;" +puts $fid " background-color: $COLOR_TEXT;" +puts $fid " color: $COLOR_BG;" +puts $fid " padding: 5px 0;" +puts $fid " padding-left: 20px;" +puts $fid " text-align: left;" +puts $fid "}" +puts $fid "h3 {" +puts $fid " font-size: 15px;" +puts $fid " font-family: sans-serif;" +puts $fid " background-color: $COLOR_LIGHTGRAY;" +puts $fid " color: $COLOR_TEXT;" +puts $fid " padding: 5px 0;" +puts $fid " padding-left: 10px;" +puts $fid " text-align: left;" +puts $fid "}" + +close $fid + +set outfid [open "$output_directory/index.html" "w"] +start_html $outfid "$title" "" "./" +puts $outfid "

$title Documentation

" +foreach genre $genre_list { + set name "genre_$genre" + puts $outfid "

[regsub -all {_} "$genre" " "]

" + foreach {k v} [set $name] { + puts $outfid "$v
" + } +} +end_html $outfid +close $outfid diff --git a/example/docgen.conf b/example/docgen.conf new file mode 100644 index 0000000..d6dd20b --- /dev/null +++ b/example/docgen.conf @@ -0,0 +1,8 @@ +# vim: syntax=tcl + +set input_directory "input" +set output_directory "output" +set title "Example Project" +set icon "icon.png" +set footer "Some good footer" +set links "nishi.boats" diff --git a/example/icon.png b/example/icon.png new file mode 100644 index 0000000..fa8a162 Binary files /dev/null and b/example/icon.png differ diff --git a/example/input/getstarted/index.md b/example/input/getstarted/index.md new file mode 100644 index 0000000..b37d843 --- /dev/null +++ b/example/input/getstarted/index.md @@ -0,0 +1,24 @@ +# Getting Started {User Guide} +blah blah blah blah + +## Download +blah blah blah.\ +blah\ +*BLAH* **BLAH** ***BLAH*** + +## Build +blah blah blah. + +## Install +blah blah: `blah blah` + +```c +int main(){ + printf("Hello, world!\n"); +} +``` + +## Links + +[![blah](/icon.png)](http://nishi.boats) +[blah](http://nishi.boats) diff --git a/example/input/getstarted2/getstarted3/index.md b/example/input/getstarted2/getstarted3/index.md new file mode 100644 index 0000000..3d7f617 --- /dev/null +++ b/example/input/getstarted2/getstarted3/index.md @@ -0,0 +1,24 @@ +# Getting Started 3 {User Guide} +blah blah blah blah + +## Download +blah blah blah.\ +blah\ +*BLAH* **BLAH** ***BLAH*** + +## Build +blah blah blah. + +## Install +blah blah: `blah blah` + +```c +int main(){ + printf("Hello, world!\n"); +} +``` + +## Links + +[![blah](/icon.png)](http://nishi.boats) +[blah](http://nishi.boats) diff --git a/example/input/getstarted2/index.md b/example/input/getstarted2/index.md new file mode 100644 index 0000000..9278716 --- /dev/null +++ b/example/input/getstarted2/index.md @@ -0,0 +1,24 @@ +# Getting Started 2 {User Guide} +blah blah blah blah + +## Download +blah blah blah.\ +blah\ +*BLAH* **BLAH** ***BLAH*** + +## Build +blah blah blah. + +## Install +blah blah: `blah blah` + +```c +int main(){ + printf("Hello, world!\n"); +} +``` + +## Links + +[![blah](/icon.png)](http://nishi.boats) +[blah](http://nishi.boats)