From 6bc2a44ba2842d6fb4ea04e464b0a16e6835f6d4 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Sun, 8 Jan 2012 14:09:47 +0100 Subject: [PATCH] Tool cxx_to_html to convert C++ source to HTML --- tool/README | 6 ++ tool/cxx_to_html | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100755 tool/cxx_to_html diff --git a/tool/README b/tool/README index f80c6d83cf..d354cd7f96 100644 --- a/tool/README +++ b/tool/README @@ -35,6 +35,12 @@ of Genode. This tool is a C++ parser used as front-end for Beautify. +:'cxx_to_html': + + This tool converts a C++ source file to a HTML view to be used for the API + documentation at the genode.org website. It relies on 'parse_cxx' to do the + actual C++ parsing. + :'fix_include_ifndef': This tool helps with assigning consistent include guards to header files. diff --git a/tool/cxx_to_html b/tool/cxx_to_html new file mode 100755 index 0000000000..3bd5e29312 --- /dev/null +++ b/tool/cxx_to_html @@ -0,0 +1,150 @@ +#!/usr/bin/tclsh + +# +# \brief Convert C++ code to html +# \author Norman Feske +# \date 2008-06-14 +# + +################################################# +## Read input and fill internal representation ## +################################################# + +## +# Find location of 'parse_cxx' +# +# We expect to find 'parse_cxx' in the same directory +# as we are located. The question is: Where are we? +## +proc parse_cxx_file { } { + global argv0 + + set path $argv0 + + if {[file type $path] == "link"} { + set path [file readlink $path] } + + set parse_cxx_file "[file dirname $path]/parse_cxx" + + if {![file exists $parse_cxx_file]} { + puts stderr "Error: Could not find 'parse_cxx' in '$path'." + exit -1 + } + return $parse_cxx_file +} + + +set input_source [lindex $argv end] +if {[catch { + if {[info exists input_token_file]} { + set tokens [exec cat $input_token_file] + } else { + set tokens [exec [parse_cxx_file] -format tokens $input_source] + } +}]} { + puts stderr "" + puts stderr "Convert C++ code to HTML" + puts stderr "\n usage: cxx_to_html \[\]" + puts stderr "" + puts stderr "The result of the conversion will be written to stdout." + puts stderr "" + exit -1; +} + +foreach token $tokens { + set name [lindex $token 0] + set line [lindex $token 1] + set text [lindex $token 2] + set tok_text($name) "$text" + set tok_line($name) $line +} + +if {![info exists tok_text(content0)]} { + puts stderr "Error: input contains no root token 'content0'." + exit -1 +} + + +proc tok_type {token} { + regexp {[a-z]+} $token type + return $type +} + + +proc line_tag {line_num} { + set line "$line_num\\\ \\\ " + if {$line_num < 1000} { set line "\\\ $line" } + if {$line_num < 100} { set line "\\\ $line" } + if {$line_num < 10} { set line "\\\ $line" } + return "$line" +} + + +## +# Output syntax tree as HTML +## +proc dump_source {{token content0}} { + global tok_text + global line_num + + set output $tok_text($token) + + set curr_tok_type [tok_type $token] + + if {$token == "content0"} { + set line_num 1 + puts -nonewline "\ \ \ 1\ \ " + incr line_num + } + + if {$curr_tok_type != "identifier"} { + puts -nonewline "" + } + + while {$output != ""} { + + # consume plain text + if {[regexp {^[^§]+} $output plain]} { + # perform character substitutions for xml compliance + regsub -all {³} $plain "\\\&" plain + regsub -all {<} $plain "\\\<" plain + regsub -all {>} $plain "\\\>" plain + regsub -all "\"" $plain "\\\"" plain + + # because of a bug in plone/zope, which somehow strips the trailing semicolon + # on html output, we cannot use ' + #regsub -all "'" $plain "\\\'" plain + + regsub -all "'" $plain "`" plain + regsub -all " " $plain "\\\ " plain + regsub -all "\t" $plain "\\\ \\\ \\\ " plain + + while {[regexp "\n" $plain dummy]} { + regsub "\n" $plain "
[line_tag $line_num]" plain + incr line_num + } + + regsub -all "\
" $plain "
\n" plain + puts -nonewline $plain + regsub {^[^§]+} $output "" output + } + + # consume token + if {[regexp {§(.+?)°} $output dummy subtoken]} { + dump_source $subtoken + regsub {§(.+?)°} $output "" output + } + } + if {$curr_tok_type != "identifier"} { + puts -nonewline "
" + } +} + +# print header +puts "
" + +# print content +dump_source + +# print footer +puts "
"