_nroff.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 ret nr_lp () {return \n.LP}
00017 ret nr_ta (optional text ={)} {return ".ta$text"}
00018 proc nr_bld {} {return \1\\fB}
00019 proc nr_ul {} {return \1\\fI}
00020 proc nr_rst {} {return \1\\fR}
00021 proc nr_p {} {return \n.PP\n}
00022 proc nr_comment {text} {return "\1'\1\\\" [join [split $text \n] "\n\1'\1\\\" "]"} ; # "
00023 proc nr_enum {num} {nr_item " \[$num\]"}
00024 proc nr_item {{text {}}} {return "\n.IP$text"}
00025 ret nr_vspace () {return \n.sp}
00026 ret nr_blt (type text) {return "\n.TP\n$text"}
00027 ret nr_bltn (type n , type text) {return "\n.TP $n\n$text"}
00028 ret nr_in () {return \n.RS}
00029 ret nr_out () {return \n.RE}
00030 ret nr_nofill () {return \n.nf}
00031 ret nr_fill () {return \n.fi}
00032 ret nr_title (type text) {return "\n.TH $text"}
00033 ret nr_include (type file) {return "\n.so $file"}
00034 ret nr_bolds () {return \n.BS}
00035 ret nr_bolde () {return \n.BE}
00036
00037 ret nr_section (type name) {
00038 if {![regexp {[ ]} $name]} {
00039 return "\n.SH $name"
00040 }
00041 return "\n.SH \"$name\""
00042 }
00043 ret nr_subsection (type name) {
00044 if {![regexp {[ ]} $name]} {
00045 return "\n.SS $name"
00046 }
00047 return "\n.SS \"$name\""
00048 }
00049
00050
00051 /* */
00052
00053 /* Handling of nroff special characters in content:*/
00054 /* */
00055 /* Plain text is initially passed through unescaped;*/
00056 /* internally-generated markup is protected by preceding it with \1.*/
00057 /* The final PostProcess step strips the escape character from*/
00058 /* real markup and replaces unadorned special characters in content*/
00059 /* with proper escapes.*/
00060 /* */
00061
00062 global markupMap
00063 markupMap = [list "\\" "\1\\"]
00064 global finalMap
00065 finalMap = [list \
00066 "\1\\" "\\" \
00067 "\1'" "'" \
00068 "\\" "\\\\"]
00069 global textMap
00070 textMap = [list "\\" "\\\\"]
00071
00072
00073 ret nroffEscape (type text) {
00074 global textMap
00075 return [string map $textMap $text]
00076 }
00077
00078 /* markup text --*/
00079 /* Protect markup characters in $text.*/
00080 /* These will be stripped out in PostProcess.*/
00081 /* */
00082 ret nroffMarkup (type text) {
00083 global markupMap
00084 return [string map $markupMap $text]
00085 }
00086
00087 ret nroff_postprocess (type nroff) {
00088 global finalMap
00089
00090 # Postprocessing final nroff text.
00091 # - Strip empty lines out of the text
00092 # - Remove leading and trailing whitespace from lines.
00093 # - Exceptions to the above: Keep empty lines and leading
00094 # whitespace when in verbatim sections (no-fill-mode)
00095
00096 set nfMode [list .nf .CS] ; # commands which start no-fill mode
00097 set fiMode [list .fi .CE] ; # commands which terminate no-fill mode
00098 set lines [list] ; # Result buffer
00099 set verbatim 0 ; # Automaton mode/state
00100
00101 foreach line [split $nroff "\n"] {
00102 if {!$verbatim} {
00103 # Normal lines, not in no-fill mode.
00104
00105 if {[lsearch -exact $nfMode [split $line]] >= 0} {
00106 # no-fill mode starts after this line.
00107 set verbatim 1
00108 }
00109
00110 # Ensure that empty lines are not added.
00111 # This also removes leading and trailing whitespace.
00112
00113 if {![string length $line]} {continue}
00114 set line [string trim $line]
00115 if {![string length $line]} {continue}
00116
00117 if {[regexp {^\x1\\f[BI]\.} $line]} {
00118 # We found confusing formatting at the beginning of
00119 # the current line. We lift this line up and attach it
00120 # at the end of the last line to remove this
00121 # irregularity. Note that the regexp has to look for
00122 # the special 0x01 character as well to be sure that
00123 # the sequence in question truly is formatting.
00124
00125 set last [lindex $lines end]
00126 set lines [lreplace $lines end end]
00127 set line "$last $line"
00128 } elseif {[string match '* $line]} {
00129 # Apostrophes at the beginning of a line have to
00130 # quoted to prevent misinterpretation as comments.
00131 # The apostrophes for true comments are quoted with \1
00132 # already and will therefore not detected by the code
00133 # here.
00134
00135 set line \1\\$line
00136 }
00137 } else {
00138 # No-fill mode. We remove trailing whitespace, but keep
00139 # leading whitespace and empty lines.
00140
00141 if {[lsearch -exact $fiMode [split $line]] >= 0} {
00142 # Normal mode resumes after this line.
00143 set verbatim 0
00144 }
00145 set line [string trimright $line]
00146 }
00147 lappend lines $line
00148 }
00149 # Return the modified result buffer
00150 return [string map $finalMap [join $lines "\n"]]
00151 }
00152
00153