File: man2md

package info (click to toggle)
owfs 3.2p3+dfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,688 kB
  • sloc: ansic: 65,085; pascal: 5,957; tcl: 2,688; makefile: 1,357; python: 1,114; sh: 891; cs: 623; php: 600; perl: 587; java: 404; ruby: 289; cpp: 105; asm: 102; xml: 53
file content (103 lines) | stat: -rwxr-xr-x 2,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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}
	}
}