104 lines (87 with data), 2.3 kB
#!/usr/bin/tclsh
##
## man2md - simple man to markdown converter
##
## supports as much of the troff syntax to create markdown formatted versions
## of the owfs manpages.
##
## (C)2016 Jan Kandziora <jjj@gmx.de>
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, version 2 of the License.
##
##
## EXAMPLE: soelim -r man3/DS18B20.man | man2md
##
## Global flags.
set ::title {}
set ::section {}
set ::subsection {}
## Formatting functions.
proc put_break {} {
puts " "
}
proc put_bold {str} {
## In "SEE ALSO" section and title subsection, link other manpages.
if {$::section eq "SEE ALSO" || $::subsection eq $::title} {
foreach {page mansection} $str {
if {$mansection ne {}} {
puts -nonewline " **\[$page $mansection\]($page)**"
} {
puts -nonewline " **$page**"
}
}
} {
puts -nonewline " **${str}**"
}
}
proc put_italic {str} {
puts -nonewline " _${str}_"
}
proc put_paragraph {} {
puts "\n"
}
proc put_section {heading} {
## Do not put in section "NAME"
if {$heading ne "NAME"} {
puts "\n\n## $heading"
}
## Remember section title.
set ::section $heading
set ::subsection {}
}
proc put_subsection {heading} {
puts "\n\n### $heading"
## Remember subsection title.
set ::subsection $heading
}
proc put_title {title mansection args} {
## Remember page title.
set ::title $title
}
proc put_indented_paragraph {indent} {
puts "\n"
}
proc put_text {line} {
puts -nonewline " $line"
}
## Go through soelim'd manpage read from stdin.
foreach line [split [read stdin] \n] {
switch -regexp -matchvar match -- $line {
{^'\\"} {}
{^\\- (.*)$} {put_text "- [lindex $match 1]"}
{^.br$} put_break
{^.B (.*)$} {put_bold [lindex $match 1]}
{^.I (.*)$} {put_italic [lindex $match 1]}
{^.LP$} -
{^.P$} -
{^.PP$} {put_paragraph}
{^.SH ?(.*)$} {put_section [lindex $match 1]}
{^.SS ?(.*)$} {put_subsection [lindex $match 1]}
{^.TH ?(.*)$} {put_title {*}[lindex $match 1]}
{^.IP ?(.*)$} {put_indented_paragraph [lindex $match 1]}
{^.TP ?(.*)$} {put_indented_paragraph [lindex $match 1]}
default {put_text $line}
}
}