# Usage of man2markdown script to convert nroff to markdown
This script parses an nroff formatted Tcl/Tk manual page, converts it into some AST (Abstract Syntax Tree) and then writes it out again as a markdown file.
When this script is called from another Tcl script (i.e. using the [source] command), you can call the procedure `::ndoc::man2markdown` with a the content of a man file to be converted as an argument (the whole script and its procedures live in the '::ndoc::' namespace). If you just have the man file, you can use: `::ndoc::man2markdown [::ndoc::readFile /path/to/my/file.n]`.
This procedure will return the resulting markdown.
When this script is called directly from the command line using tclsh, specifying an nroff file as an argument will call the `[::ndoc::man2markdown]` procedure on the file content and return the markdown on stdout.
When not specifying an argument, it runs in test mode, converting a specific manual page from within the repository structure which this script is a part of. The result is written to stdout.
When run with two directory names, it will convert all nroff files in the (existing) first directory and put the result as markdown files into the second directory (which will be created if it does not exist). This should be used to produce the current status of afairs into doc/markdown/:
```
# (from the tools directory)
tclsh man2markdown.tcl ../doc/nroff-original ../doc/markdown/
```
The last invocation can be used to check what changed with respect to the last `fossil commit`. Any change reported by fossil (before committing) is a change that might have issues compared to the previous version, so it should be checked before the commit and amended before the commit!
# Usage of Pandoc to convert generated markdown
We use Pandoc to convert the markdown sources to both nroff and HTML as long as no converter exists in Tcl which can handle this well. As the dollar sign is often used in the manual pages to mark variables and we do not need TeX math, we disable the tex_math_dollars extension in Pandoc.
## Conversion to nroff
Basically, you can use this to generate nroff which renders most of what you will need.
```
pandoc -f markdown-tex_math_dollars -t man mypage.md -o mypage.n
```
For a closer representation use the markdown2nroff.tcl script.
## Conversion to HTML
```
pandoc -f markdown-tex_math_dollars -t html mypage.md -o mypage.html
```
For a closer representation use the markdown2html.tcl script.
# Status of conversion process for Tcl
The tcl/doc directory contains 248 manual pages which are divided as follows:
section | number of documents | initial conversion | final conversion
--------|---------------------|--------------------|-----------------
1 | 1 | 1 | 1
3 | 108 | 108 | 108
n | 145 | 145 | 145
Final conversion of these files in the n section is currently done:
1. after
2. append
3. apply
4. array
5. bgerror
6. binary
7. break
8. buildinfo
9. callback
10. catch
11. cd
12. chan (indentation for [chan configure] is not ok -> will be dealt with when restructuring manual pages after TIP 700)
13. class
14. classvariable
15. clock -> we may need to use '(1)' instead of '[1]' for referencing list items as this might be misinterpreted during conversion to HTML
16. close
17. concat
18. configurable
19. const
20. continue
21. cookiejar
22. copy
23. coroutine
24. dde
25. define
26. dict
27. encoding
28. eof
29. error
30. eval
31. exec (the section PORTABILITY ISSUES need manual conversion into markdown subsections)
32. exit
33. expr (the section OPERANDS needs manual adaptation after conversion to markdown)
34. fblocked
35. fconfigure
36. fcopy
37. file
38. fileevent
39. filename
40. flush
41. for
42. foreach
43. format
44. fpclassify
45. gets
46. glob
47. global
48. history
49. http (manual postprocessing needed: the options of some subcommands need more indentation which the script didn't catch)
50. idna
51. if
52. incr
53. info (manual postprocessing needed: some cmd names are links, others are option names etc.)
54. interp (the list of safe commands needs manual formatting)
55. join
56. lappend
57. lassign
58. ledit
59. library (might need manual postprocessing ...)
60. lindex
61. link
62. linsert
63. list
64. llength
65. lmap
66. load
67. lpop
68. lrange
69. lremove
70. lrepeat
71. lreplace
72. lreverse
73. lsearch
74. lseq
75. lset
76. lsort
77. mathfunc (list of functions needs other formatting, the math function syntax should perhaps not use {.cmd})
78. mathop (list of operators needs other formatting, the math operator syntax should perhaps not use {.cmd
79. memory
80. msgcat (check the TIP499 marker)
81. my
82. namespace
83. next
84. object
85. open
86. package (the deep list in section "Requirement" needs manual adjustment)
87. packagens
88. pid
89. pkg_mkIndex
90. platform
91. platform_shell
92. prefix
93. proc
94. process (the command `process status` needs manual reformatting)
95. puts
96. pwd
97. re_syntax (needs extensive manual formatting of brackets and backslashes)
98. read
99. refchan
100. regexp
101. registry
102. regsub
103. rename
104. return
105. safe (the links to other manual pages need manual correction here, there are just too many special cases!)
106. scan
107. seek
108. self
109. set
110. singleton
111. socket
112. source
113. split
114. string
115. subst
116. switch
117. tailcall
118. tcltest
119. tclvars
120. tell
121. throw
122. time
123. timerate
124. tm
125. trace
126. transchan
127. try
128. unicode (check the TIP726 markers)
129. unknown
130. unload
131. unset
132. update
133. uplevel
134. upvar
135. variable
136. vwait
137. while
138. zipfs
139. zlib (will need some manual adjustments)
New in Tcl 9.1:
140. divmod
141. frexp
142. lfilter
143. modf
144. remquo
145. timer
Now the 3 section:
1. Access
2. AddErrInfo
3. Alloc
4. AllowExc
5. AppInit
6. AssocData
7. Async
8. BackgdErr
9. BoolObj
10. ByteArrObj
11. CallDel
12. Cancel
13. ChnlStack
14. Class
15. CmdCmpIt
16. Concat (Concat3)
17. CrtAlias
18. CrtChannel
19. CrtChnlHdlr
20. CrtCloseHndlr
21. CrtCommand
22. CrtFileHndlr
23. CrtInterp
24. CrtObjCmd
25. CrtTimerHndlr
26. CrtTrace
27. DString
28. DetachPids
29. DictObj
30. DoOneEvent
31. DoWhenIdle
32. DoubleObj
33. DumpActiveMemory
34. Encoding
35. Ensemble
36. Environment
37. Eval
38. Exit
39. ExprLong
40. ExprLongObj
41. FileSystem
42. FindExec
43. GetCwd
44. GetHostName
45. GetIndex
46. GetInt
47. GetOpnFl
48. GetStdChan
49. GetTime
50. GetVersion
51. Hash
52. Init (perhaps the typedef line in the synopsis needs some manual formatting)
53. InitStubs
54. InitSubSyst
55. IntObj
56. Limit
57. LinkVar
58. ListObj
59. Load3
60. Method
61. NRE
62. Namespace
63. Notifier
64. Number
65. OOInitStubs.md
66. Object3
67. ObjectType
68. OpenFileChnl
69. OpenTcp
70. Panic
71. ParseArgs
72. ParseCmd
73. PkgRequire
74. Preserve
75. PrintDbl
76. RecEvalObj
77. RecordEval
78. RegConfig
79. RegExp3
80. SaveInterpState
81. SetChanErr
82. SetErrno
83. SetRectLmt
84. SetResult
85. SetVar
86. Signal
87. Sleep
88. SourceRCFile
89. SplitList
90. SplitPath
91. StaticLibrary
92. StdChannels (needs manula adjustment of the numbered list)
93. StrMatch
94. StringObj
95. SubstObj
96. TCL_MEM_DEBUG
97. TclZlib
98. Tcl_Main
99. Thread
100. ToUpper
101. TraceCmd
102. TraceVar
103. Translate
104. UniCharIsAlpha
105. UnicodeNormalize
106. UpVar3
107. Utf
108. WrongNumArgs
Now the 1 section:
1. tclsh
# Status of conversion process for Tk
The tk/doc directory contains 181 manual pages which are divided as follows:
section | number of documents | initial conversion | final conversion
--------|---------------------|--------------------|-----------------
1 | 1 | 0 | 0
3 | 87 | 0 | 0
n | 93 | 1 | 0
Final conversion of these files in the n section is currently done:
...
# Open issues
This section lists the shortcoming which are still present in the pages that already have an initial markdown version.
## General
- handling of '\fB\e\fIk\fR' (see test script) not correct yet
- no handling of ".AS" and ".AP" macros yet
- general alignment of the wording in the manual pages to the one in the Tcl error messages emitted (e.g. `dict for` uses "dictionaryValue body" in the manual page but "dictionary script" in the C code)
- alignment of changed wording/presentation of Tcl syntax within the C code error messages emitted by Tcl when using wrong Tcl syntax
(example: `dict for {keyVarName valueVarName} dictionary script` --> `dict for varaibleList dictionary script`)
- use camelCase consistently for Tcl syntax arguments
- handling of the "|" character for alternatives (see `memory init on|off`) is not yet good, leaves it as `on=|§off`
- handling of links does not yet use all the exceptions defined in tools/tcltk-man2html.tcl and tools/tclsk-man2html-utils.tcl (for now only exclude_refs_map is used)
- not all instances of "TIP" infos are preserved (especially when inside a combined .RE/.PP context as in msgcat.n, so grep all uses and check manually after automatic conversion)
## Specific pages
- RegConfig.3: besides the C API, this page describes Tcl-level commands that are created. These use a non-standard format in the form of ::, followed by an .arg, followed by ::, followed by a .lit – all in one word!
# Implemented features in conversion script
List of nroff commands/macros currently implemented (partly not a complete implementation):
- .\" (comments)
- '\" (comments)
- \(-> (right arrow, converted to \u2192)
- \\N'34' (literal double quote)
- backslash escapes "e" and "c"
- . (single dot as placeholder)
- \\fB (bold text)
- \\fI (italic text)
- \\fR (reset font info)
- \\fP (treated the same as \\fB)
- \\(fm (prime symbol)
- .QW (text in quotes)
- .PQ (text in quotes + paranthesis)
- .QR (text in quotes + dash between elements)
- .so (include files)
- .BS .BE (start and end of box enclosure)
- .AS (maximum sizes of arguments for setting tab stops)
- .ta (set tab stops)
- .RS .RE (relative inset, i.e. indentation)
- .nf .fi (turn off/on filling of lines, only used in SYNOPSIS and oo CLASS HIERARCHY, the effect being similar to <pre> and <code> for HTML)
- .TH (title heading)
- .TP (tagged paragraph)
- .IP (indented paragaph)
- .PP (paragraph)
- .SH (section header)
- .SS (subsection header)
- .VS .VE (start and end of vertical line)
- .CS .CE (start and end of code block)
- .sp (inserts vertical space) > this is treated individually (currently by mdExceptions)
- .ta (tabulator)
- .DS DE (display section)
- .MT (here used for "" display)
# Manual work to be done after final conversion
After the final round of conversion from nroff to Markdown, some manual work needs to be done that is not covered by the conversion script
- subdivide certain manual pages (the nroff files only have sections and subsection, in the markdown version, also subsubsections are needed in some pages)
- find all pages where nested definition lists occur (e.g. Tcl.md) and consider using subsections instead
- look at the sections marked as '{.info DISPLAY="yes"}' as these need a proper visual representation