1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* mdoc2man.rb: Make this work as a library.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3371 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2003-01-20 10:44:10 +00:00
parent 4a464c1fa2
commit c1d00ec453
2 changed files with 345 additions and 325 deletions

View file

@ -1,3 +1,7 @@
Mon Jan 20 19:43:41 2003 Akinori MUSHA <knu@iDaemons.org>
* mdoc2man.rb: Make this work as a library.
Mon Jan 20 18:22:40 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
* eval.c (rb_f_require): purge too many goto's.

View file

@ -42,18 +42,39 @@
### $Id$
###
$name = $date = $id = nil
$refauthors = $reftitle = $refissue = $refdate = $refopt = nil
class Mdoc2Man
def initialize
@name = @date = @id = nil
@refauthors = @reftitle = @refissue = @refdate = @refopt = nil
$optlist = 0 ### 1 = bullet, 2 = enum, 3 = tag, 4 = item
$oldoptlist = 0
$nospace = 0 ### 0, 1, 2
$enum = 0
$synopsis = true
$reference = false
$ext = false
$extopt = false
$literal = false
@optlist = 0 ### 1 = bullet, 2 = enum, 3 = tag, 4 = item
@oldoptlist = 0
@nospace = 0 ### 0, 1, 2
@enum = 0
@synopsis = true
@reference = false
@ext = false
@extopt = false
@literal = false
end
def mdoc2man(i, o)
i.each { |line|
if /^\./ !~ line
o.print line
o.print ".br\n" if @literal
next
end
line.slice!(0, 1)
next if /\\"/ =~ line
line = parse_macro(line) and o.print line
}
initialize
end
def parse_macro(line)
words = line.split
@ -67,22 +88,22 @@ def parse_macro(line)
when 'Li', 'Pf'
next
when 'Xo'
$ext = true
@ext = true
retval << ' ' unless retval.empty? || /[\n ]\z/ =~ retval
next
when 'Xc'
$ext = false
retval << "\n" unless $extopt
@ext = false
retval << "\n" unless @extopt
break
when 'Bd'
$literal = true if words[0] == '-literal'
@literal = true if words[0] == '-literal'
retval << "\n"
break
when 'Ed'
$literal = false
@literal = false
break
when 'Ns'
$nospace = 1 if $nospace == 0
@nospace = 1 if @nospace == 0
retval.chomp!(' ')
next
when 'No'
@ -96,56 +117,56 @@ def parse_macro(line)
end until words.empty? || /^[\.,]/ =~ words[0]
retval.chomp!(' ')
retval << '\'\''
$nospace = 1 if $nospace == 0 && /^[\.,]/ =~ words[0]
@nospace = 1 if @nospace == 0 && /^[\.,]/ =~ words[0]
next
when 'Sq', 'Ql'
retval << '`' << words.shift << '\''
$nospace = 1 if $nospace == 0 && /^[\.,]/ =~ words[0]
@nospace = 1 if @nospace == 0 && /^[\.,]/ =~ words[0]
next
# when 'Ic'
# retval << '\\fB' << words.shift << '\\fP'
# next
when 'Oo'
#retval << "[\\c\n"
$extopt = true
$nospace = 1 if $nospace == 0
@extopt = true
@nospace = 1 if @nospace == 0
retval << '['
next
when 'Oc'
$extopt = false
@extopt = false
retval << ']'
next
end
retval << ' ' if $nospace == 0 && !(retval.empty? || /[\n ]\z/ =~ retval)
$nospace = 0 if $nospace == 1
retval << ' ' if @nospace == 0 && !(retval.empty? || /[\n ]\z/ =~ retval)
@nospace = 0 if @nospace == 1
case word
when 'Dd'
$date = words.join(' ')
@date = words.join(' ')
return nil
when 'Dt'
$id = words.join(' ')
@id = words.join(' ')
return nil
when 'Os'
retval << '.TH ' << $id << ' "' << $date << '" "' <<
retval << '.TH ' << @id << ' "' << @date << '" "' <<
words.join(' ') << '"'
break
when 'Sh'
retval << '.SH'
$synopsis = (words[0] == 'SYNOPSIS')
@synopsis = (words[0] == 'SYNOPSIS')
next
when 'Xr'
retval << '\\fB' << words.shift <<
'\\fP(' << words.shift << ')' << words.shift
break
when 'Rs'
$refauthors = []
$reftitle = ''
$refissue = ''
$refdate = ''
$refopt = ''
$reference = true
@refauthors = []
@reftitle = ''
@refissue = ''
@refdate = ''
@refopt = ''
@reference = true
break
when 'Re'
retval << "\n"
@ -158,62 +179,62 @@ def parse_macro(line)
retval << @refauthors.shift
# title
retval << ', \\fI' << $reftitle << '\\fP'
retval << ', \\fI' << @reftitle << '\\fP'
# issue
retval << ', ' << $refissue unless $refissue.empty?
retval << ', ' << @refissue unless @refissue.empty?
# date
retval << ', ' << $refdate unless $refdate.empty?
retval << ', ' << @refdate unless @refdate.empty?
# optional info
retval << ', ' << $refopt unless $refopt.empty?
retval << ', ' << @refopt unless @refopt.empty?
retval << ".\n"
$reference = false
@reference = false
break
when 'Ux'
retval << "UNIX"
next
end
if $reference
if @reference
case word
when '%A'
@refauthors.unshift(words.join(' '))
break
when '%T'
$reftitle = words.join(' ')
$reftitle.sub!(/^"/, '')
$reftitle.sub!(/"$/, '')
@reftitle = words.join(' ')
@reftitle.sub!(/^"/, '')
@reftitle.sub!(/"$/, '')
break
when '%N'
$refissue = words.join(' ')
@refissue = words.join(' ')
break
when '%D'
$refdate = words.join(' ')
@refdate = words.join(' ')
break
when '%O'
$refopt = words.join(' ')
@refopt = words.join(' ')
break
end
end
case word
when 'Nm'
name = words.empty? ? $name : words.shift
$name ||= name
retval << ".br\n" if $synopsis
name = words.empty? ? @name : words.shift
@name ||= name
retval << ".br\n" if @synopsis
retval << "\\fB" << name << "\\fP"
$nospace = 1 if $nospace == 0 && /^[\.,]/ =~ words[0]
@nospace = 1 if @nospace == 0 && /^[\.,]/ =~ words[0]
next
when 'Nd'
retval << '\\-'
next
when 'Fl'
retval << '\\fB\\-' << words.shift << '\\fP'
$nospace = 1 if $nospace == 0 && /^[\.,]/ =~ words[0]
@nospace = 1 if @nospace == 0 && /^[\.,]/ =~ words[0]
next
when 'Ar'
retval << '\\fI'
@ -224,7 +245,7 @@ def parse_macro(line)
while words[0] == '|'
retval << ' ' << words.shift << ' \\fI' << words.shift << '\\fP'
end
$nospace = 1 if $nospace == 0 && /^[\.,]/ =~ words[0]
@nospace = 1 if @nospace == 0 && /^[\.,]/ =~ words[0]
next
end
when 'Cm'
@ -235,7 +256,7 @@ def parse_macro(line)
next
when 'Op'
option = true
$nospace = 1 if $nospace == 0
@nospace = 1 if @nospace == 0
retval << '['
# words.push(words.pop + ']')
next
@ -254,7 +275,7 @@ def parse_macro(line)
while /^[\.,:;)]$/ =~ words[0]
retval << words.shift
end
# $nospace = 1 if $nospace == 0 && /^[\.,]/ =~ words[0]
# @nospace = 1 if @nospace == 0 && /^[\.,]/ =~ words[0]
next
end
@ -267,7 +288,7 @@ def parse_macro(line)
next
when 'Pq'
retval << '('
$nospace = 1
@nospace = 1
parens = true
next
when 'Sx', 'Sy'
@ -289,7 +310,7 @@ def parse_macro(line)
retval << words.shift
end
retval << ' ' if $nospace == 0
retval << ' ' if @nospace == 0
end
retval.chomp!(' ')
@ -297,35 +318,35 @@ def parse_macro(line)
retval << words.shift unless words.empty?
break
when 'Bl'
$oldoptlist = $optlist
@oldoptlist = @optlist
case words[0]
when '-bullet'
$optlist = 1
@optlist = 1
when '-enum'
$optlist = 2
$enum = 0
@optlist = 2
@enum = 0
when '-tag'
$optlist = 3
@optlist = 3
when '-item'
$optlist = 4
@optlist = 4
end
break
when 'El'
$optlist = $oldoptlist
@optlist = @oldoptlist
next
end
if $optlist != 0 && word == 'It'
case $optlist
if @optlist != 0 && word == 'It'
case @optlist
when 1
# bullets
retval << '.IP \\(bu'
when 2
# enum
$enum += 1
retval << '.IP ' << $enum << '.'
@enum += 1
retval << '.IP ' << @enum << '.'
when 3
# tags
retval << ".TP\n"
@ -346,10 +367,10 @@ def parse_macro(line)
when 'Sm'
case words[0]
when 'off'
$nospace = 2
@nospace = 2
when 'on'
# retval << "\n"
$nospace = 0
@nospace = 0
end
words.shift
next
@ -367,25 +388,20 @@ def parse_macro(line)
retval << ']' if option
# retval << ' ' unless $nospace == 0 || retval.empty? || /\n\z/ =~ retval
# retval << ' ' unless @nospace == 0 || retval.empty? || /\n\z/ =~ retval
retval << ' ' unless !$ext || $extopt || / $/ =~ retval
retval << ' ' unless !@ext || @extopt || / $/ =~ retval
retval << "\n" unless $ext || $extopt || retval.empty? || /\n\z/ =~ retval
retval << "\n" unless @ext || @extopt || retval.empty? || /\n\z/ =~ retval
return retval
end
ARGF.each { |line|
if /^\./ !~ line
print line
print ".br\n" if $literal
next
def self.mdoc2man(i, o)
new.mdoc2man(i, o)
end
end
line.slice!(0, 1)
next if /\\"/ =~ line
line = parse_macro(line) and print line
}
if $0 == __FILE__
Mdoc2Man.mdoc2man(ARGF, STDOUT)
end