rdtool 0.6.38 → 0.6.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/rdswap.rb CHANGED
@@ -1,102 +1,151 @@
1
- #! /usr/bin/ruby1.9.1 -s
1
+ #!/usr/bin/env ruby
2
2
  #######
3
3
  # rdswap.rb (c) C.Hintze <c.hintze@gmx.net> 30.08.1999
4
4
  #######
5
5
 
6
+ require "optparse"
6
7
 
7
- require "ostruct";
8
-
9
- if ARGV.size < 2 and not ($h or $help)
10
- print "Wrong # of paramter! Use `-h' for help.\n";
11
- exit 1;
12
- elsif $h or $help
13
- print eval('"'+DATA.read+'"');
14
- exit 0;
15
- end
16
-
17
- srcfile = ARGV.select{|fn| fn =~ /\.rb$/o};
18
- case srcfile.size
19
- when 0
20
- $stderr.print "Warning: No `.rb' file given! Take first file as source.\n";
21
- srcfile = ARGV[0];
22
- when 1
23
- srcfile = srcfile[0];
24
- else
25
- print "Sorry! Only one source file (`.rb') allowed!\n";
26
- exit(1);
27
- end
8
+ RDDocumentBlock = Struct.new(:kind, :lines)
28
9
 
29
- docs = {};
30
- srcs = {};
31
-
32
- rddoc, rddocs = nil, [];
33
- source, sources = [], [[]];
34
-
35
- while gets
36
- lang = $1 if File::basename(String($<)) =~ /^.*?([^.]+)$/o;
37
- if /^=begin/o .. /^=end/o
38
- title = $2 if /^=begin(\s+(.*))?$/o;
39
- unless rddoc.nil?
40
- unless /^=end/o
41
- rddoc.lines << $_;
42
- else
43
- rddocs << rddoc;
44
- sources << [];
45
- rddoc = nil;
46
- title = nil;
47
- end
48
- else # New RD block found! Instantiate data container.
49
- rddoc = OpenStruct.new
50
- rddoc.kind, rddoc.lines = title, [];
51
- end
52
- else # It is not a RD block means, it is a source line!
53
- sources[-1] << $_;
10
+ class RDSwap
11
+ def initialize(argv, stdout: $stdout, stderr: $stderr)
12
+ @argv = argv.dup
13
+ @stdout = stdout
14
+ @stderr = stderr
15
+ @verbose = false
16
+ end
17
+
18
+ def run
19
+ parse_options!
20
+
21
+ if @argv.size < 2
22
+ @stdout.print("Wrong # of paramter! Use `-h' for help.\n")
23
+ return 1
54
24
  end
55
- if $<.eof? # One file finished. Remember data and proceed.
56
- docs[lang] = rddocs;
57
- srcs[lang] = sources;
58
- rddoc, rddocs = nil, [];
59
- source, sources = [], [[]];
25
+
26
+ source_file = detect_source_file(@argv)
27
+ docs, srcs = load_documents(@argv)
28
+
29
+ source_blocks = srcs["rb"]
30
+ source_docs = docs["rb"]
31
+ raise "No source file content found." unless source_blocks && source_docs
32
+
33
+ (docs.keys - ["rb"]).each do |lang|
34
+ write_translation(source_file, lang, source_blocks, source_docs, docs[lang])
35
+ end
36
+
37
+ 0
38
+ end
39
+
40
+ private
41
+
42
+ def parse_options!
43
+ OptionParser.new do |opts|
44
+ opts.on("-h", "--help") do
45
+ @stdout.print(help_text)
46
+ raise SystemExit.new(0)
47
+ end
48
+
49
+ opts.on("-v", "--verbose") do
50
+ @verbose = true
51
+ end
52
+ end.parse!(@argv)
53
+ end
54
+
55
+ def help_text
56
+ HELP_TEXT % { program_name: File.basename($PROGRAM_NAME) }
57
+ end
58
+
59
+ def detect_source_file(paths)
60
+ source_files = paths.select { |path| path.end_with?(".rb") }
61
+ case source_files.size
62
+ when 0
63
+ @stderr.print "Warning: No `.rb' file given! Take first file as source.\n"
64
+ paths.first
65
+ when 1
66
+ source_files.first
67
+ else
68
+ @stdout.print "Sorry! Only one source file (`.rb') allowed!\n"
69
+ raise SystemExit.new(1)
70
+ end
71
+ end
72
+
73
+ def load_documents(paths)
74
+ docs = {}
75
+ srcs = {}
76
+
77
+ paths.each do |path|
78
+ lang = File.basename(path).split(".").last
79
+ docs[lang], srcs[lang] = parse_file(path)
60
80
  end
61
- end
62
81
 
63
- langs = docs.keys;
64
- langs.delete("rb"); # `rb' is not a language but the script!
65
- source = srcs["rb"]; # Assign it for preventing later look-ups
66
- srcdoc = docs["rb"];
67
- sourcesize = source.size; # Do not recalculate size again and again.
68
- srcdocsize = srcdoc.size;
69
-
70
- for lang in langs
71
- docblk = docs[lang];
72
- max = [sourcesize, srcdocsize, docblk.size].max;
73
- filename = File.join(srcfile+"."+lang);
74
- open(filename, "w+") do |fd|
75
- j = 0;
76
- for i in 0...max # Goto every block; be it source or RD.
77
- fd.print source[i].join unless source[i].nil? || source[i].empty?;
78
- sblk, dblk = srcdoc[i], docblk[j];
79
- blk = (dblk and (dblk.kind == sblk.kind)) ? dblk : sblk;
80
- next unless blk;
81
- j += 1 if blk == dblk;
82
- fd.print "=begin #{blk && blk.kind}\n", blk.lines.join, "=end\n";
82
+ [docs, srcs]
83
+ end
84
+
85
+ def parse_file(path)
86
+ rddocs = []
87
+ sources = [[]]
88
+ current_doc = nil
89
+
90
+ File.foreach(path) do |line|
91
+ if current_doc
92
+ if line.start_with?("=end")
93
+ rddocs << current_doc
94
+ sources << []
95
+ current_doc = nil
96
+ else
97
+ current_doc.lines << line
83
98
  end
99
+ next
100
+ end
101
+
102
+ if (match = /\A=begin(?:\s+(.*))?\s*\z/.match(line))
103
+ current_doc = RDDocumentBlock.new(match[1], [])
104
+ else
105
+ sources[-1] << line
106
+ end
84
107
  end
85
- print "File `#{filename}' created.\n" if $v;
86
- end
87
108
 
88
- exit(0);
109
+ [rddocs, sources]
110
+ end
111
+
112
+ def write_translation(source_file, lang, source_blocks, source_docs, translated_docs)
113
+ max = [source_blocks.size, source_docs.size, translated_docs.size].max
114
+ output_path = "#{source_file}.#{lang}"
115
+
116
+ File.open(output_path, "w") do |fd|
117
+ translated_index = 0
118
+
119
+ (0...max).each do |i|
120
+ fd.print(source_blocks[i].join) if source_blocks[i] && !source_blocks[i].empty?
121
+
122
+ source_doc = source_docs[i]
123
+ translated_doc = translated_docs[translated_index]
124
+ block = if source_doc && translated_doc && translated_doc.kind == source_doc.kind
125
+ translated_index += 1
126
+ translated_doc
127
+ else
128
+ source_doc
129
+ end
130
+ next unless block
131
+
132
+ fd.print "=begin #{block.kind}\n", block.lines.join, "=end\n"
133
+ end
134
+ end
89
135
 
90
- __END__
136
+ @stdout.print "File `#{output_path}' created.\n" if @verbose
137
+ end
138
+ end
91
139
 
140
+ HELP_TEXT = <<~TEXT
92
141
  Purpose:
93
142
  This tool is written to support you to write multi-language documents
94
143
  using the Ruby-Document-Format (RD).
95
144
 
96
145
  The idea for such a tool was originated by
97
-
98
- Minero Aoki <aamine@dp.u-netsurf.ne.jp>,
99
-
146
+
147
+ Minero Aoki <aamine@dp.u-netsurf.ne.jp>,
148
+
100
149
  how has thought about, how to make life easier for developers who have to
101
150
  write and maintain scripts in more than one language.
102
151
 
@@ -142,10 +191,10 @@ How does it work?
142
191
  blub blub
143
192
  =end
144
193
  :
145
-
194
+
146
195
  the first block would be of type `nil' and the second one of type `whatever
147
196
  or not'.
148
-
197
+
149
198
  Block types are important for the translation. If a source will be
150
199
  generated from a script and a translation file, only these blocks are taken
151
200
  from the translation files, that comes in the right sequence *and* contains
@@ -188,9 +237,9 @@ How does it work?
188
237
 
189
238
  That means, if the first block of `sample.de' would be of type e.g. `Never
190
239
  match', then no block would ever be taken to replace anyone of `sample.rb'.
191
-
240
+
192
241
  Syntax:
193
- #{File::basename $0} [-h|-v] <filename>...
242
+ %{program_name} [-h|-v] <filename>...
194
243
 
195
244
  Whereby:
196
245
  -h shows this help text.
@@ -198,10 +247,17 @@ Whereby:
198
247
  <filename> Means a file, that contains RD and/or Ruby code.
199
248
 
200
249
  Examples:
201
- #{File::basename $0} -v sample.rb sample.ja sample.de
202
- #{File::basename $0} -v sample.ja sample.rb sample.de
203
- #{File::basename $0} -v sample.ja sample.de sample.rb
204
- #{File::basename $0} -v sample.??
250
+ %{program_name} -v sample.rb sample.ja sample.de
251
+ %{program_name} -v sample.ja sample.rb sample.de
252
+ %{program_name} -v sample.ja sample.de sample.rb
253
+ %{program_name} -v sample.??
205
254
 
206
255
  Author:
207
256
  Clemens Hintze <c.hintze@gmx.net>.
257
+ TEXT
258
+
259
+ begin
260
+ exit(RDSwap.new(ARGV).run)
261
+ rescue SystemExit => e
262
+ raise e
263
+ end
data/lib/rd/filter.rb CHANGED
@@ -192,7 +192,7 @@ module RD
192
192
 
193
193
  alias tell pos
194
194
 
195
- def << (arg)
195
+ def <<(arg)
196
196
  begin
197
197
  @content << arg.to_s
198
198
  self
@@ -7,7 +7,7 @@ Licence: Ruby's License or GPL-2+
7
7
  =end
8
8
  require 'rd/rd2html-opt'
9
9
 
10
- q = ARGV.options
10
+ q = $RD2_OptionParser || ARGV.options
11
11
 
12
12
  q.on_tail("--ref-extension") do
13
13
  $Visitor.opt_ref_extension = true
@@ -4,7 +4,7 @@ sub-OptionParser for rd2html-lib.rb.
4
4
  =end
5
5
  require "optparse"
6
6
 
7
- q = ARGV.options
7
+ q = $RD2_OptionParser || ARGV.options
8
8
 
9
9
  q.on_tail("rd2html-lib options:")
10
10
 
@@ -63,5 +63,3 @@ q.on_tail("--html-link-rev=REV",
63
63
  end
64
64
  end
65
65
 
66
-
67
-
@@ -0,0 +1,118 @@
1
+ =begin
2
+ = rd2html5-lib.rb
3
+ =end
4
+
5
+ require "rd/rd2html-lib"
6
+
7
+ module RD
8
+ class RD2HTML5Visitor < RD2HTMLVisitor
9
+ SYSTEM_NAME = "RDtool -- RD2HTML5Visitor"
10
+ SYSTEM_VERSION = "$Version: " + RD::VERSION + "$"
11
+ VERSION = Version.new_from_version_string(SYSTEM_NAME, SYSTEM_VERSION)
12
+
13
+ def self.version
14
+ VERSION
15
+ end
16
+
17
+ def xml_decl
18
+ nil
19
+ end
20
+ private :xml_decl
21
+
22
+ def apply_to_DocumentElement(element, content)
23
+ ret = ""
24
+ ret << doctype_decl + "\n"
25
+ ret << html_open_tag + "\n"
26
+ ret << html_head + "\n"
27
+ ret << html_body(content) + "\n"
28
+ ret << "</html>\n"
29
+ ret
30
+ end
31
+
32
+ def doctype_decl
33
+ "<!DOCTYPE html>"
34
+ end
35
+ private :doctype_decl
36
+
37
+ def html_open_tag
38
+ lang_attr = %[ lang="#{@lang}"] if @lang
39
+ %|<html#{lang_attr}>|
40
+ end
41
+ private :html_open_tag
42
+
43
+ def html_content_type
44
+ %Q[<meta charset="#{@charset}">] if @charset
45
+ end
46
+ private :html_content_type
47
+
48
+ def apply_to_Headline(element, title)
49
+ anchor = get_anchor(element)
50
+ label = hyphen_escape(element.label)
51
+ title = title.join("")
52
+ %Q[<h#{element.level} id="#{anchor}">#{title}</h#{element.level}>] +
53
+ %Q[<!-- RDLabel: "#{label}" -->]
54
+ end
55
+
56
+ def apply_to_DescListItem(element, term, description)
57
+ anchor = get_anchor(element.term)
58
+ label = hyphen_escape(element.label)
59
+ term = term.join("")
60
+ if description.empty?
61
+ %Q[<dt id="#{anchor}">#{term}</dt><!-- RDLabel: "#{label}" -->]
62
+ else
63
+ %Q[<dt id="#{anchor}">#{term}</dt><!-- RDLabel: "#{label}" -->\n] +
64
+ %Q[<dd>\n#{description.join("\n").chomp}\n</dd>]
65
+ end
66
+ end
67
+
68
+ def apply_to_MethodListItem(element, term, description)
69
+ term = parse_method(term)
70
+ anchor = get_anchor(element.term)
71
+ label = hyphen_escape(element.label)
72
+ if description.empty?
73
+ %Q[<dt id="#{anchor}"><code>#{term}</code></dt><!-- RDLabel: "#{label}" -->]
74
+ else
75
+ %Q[<dt id="#{anchor}"><code>#{term}</code></dt><!-- RDLabel: "#{label}" -->\n] +
76
+ %Q[<dd>\n#{description.join("\n")}</dd>]
77
+ end
78
+ end
79
+
80
+ def apply_to_Index(element, content)
81
+ tmp = []
82
+ element.each do |i|
83
+ tmp.push(i) if i.is_a?(String)
84
+ end
85
+ key = meta_char_escape(tmp.join(""))
86
+ if @index.has_key?(key)
87
+ %Q[<!-- Index, but conflict -->#{content.join("")}<!-- Index end -->]
88
+ else
89
+ num = @index[key] = @index.size
90
+ anchor = a_name("index", num)
91
+ %Q[<span id="#{anchor}">#{content.join("")}</span>]
92
+ end
93
+ end
94
+
95
+ def apply_to_Footnote(element, content)
96
+ num = get_footnote_num(element)
97
+ raise ArgumentError, "[BUG?] #{element} is not registered." unless num
98
+
99
+ add_foottext(num, content)
100
+ anchor = a_name("footmark", num)
101
+ href = a_name("foottext", num)
102
+ %Q|<a id="#{anchor}" href="##{href}"><sup><small>*#{num}</small></sup></a>|
103
+ end
104
+
105
+ def apply_to_Foottext(element, content)
106
+ num = get_footnote_num(element)
107
+ raise ArgumentError, "[BUG] #{element} isn't registered." unless num
108
+ anchor = a_name("foottext", num)
109
+ href = a_name("footmark", num)
110
+ content = content.join("")
111
+ %|<a id="#{anchor}" href="##{href}"><sup><small>*#{num}</small></sup></a>| +
112
+ %|<small>#{content}</small><br>|
113
+ end
114
+ end
115
+ end
116
+
117
+ $Visitor_Class = RD::RD2HTML5Visitor
118
+ $RD2_Sub_OptionParser = "rd/rd2html-opt"
data/lib/rd/rd2man-lib.rb CHANGED
@@ -28,10 +28,13 @@ module RD
28
28
  OUTPUT_SUFFIX = "1"
29
29
  INCLUDE_SUFFIX = ["1"]
30
30
 
31
+ attr_accessor :man_section
32
+
31
33
  def initialize
32
34
  @enumcounter = 0
33
35
  @index = {}
34
36
  @filename = nil
37
+ @man_section = OUTPUT_SUFFIX
35
38
  end
36
39
 
37
40
  def visit(tree)
@@ -45,7 +48,7 @@ module RD
45
48
  title = title.sub(/\.rd$/i, '').upcase
46
49
  <<"EOT"
47
50
  .\\" DO NOT MODIFY THIS FILE! it was generated by rd2
48
- .TH #{title} 1 "#{Time.now.strftime '%B %Y'}"
51
+ .TH #{title} #{output_suffix} "#{Time.now.strftime '%Y-%m-%d'}"
49
52
  #{content}
50
53
  EOT
51
54
  end # "
@@ -60,13 +63,25 @@ EOT
60
63
  def apply_to_TextBlock(element, content)
61
64
  if RD::DescListItem === element.parent ||
62
65
  RD::ItemListItem === element.parent ||
63
- RD::EnumListItem === element.parent
66
+ RD::EnumListItem === element.parent ||
67
+ first_textblock_in_section?(element)
64
68
  return content.join
65
69
  else
66
70
  return ".PP\n" + content.join
67
71
  end
68
72
  end
69
73
 
74
+ def first_textblock_in_section?(element)
75
+ parent = element.parent
76
+ return false unless parent.respond_to?(:children)
77
+
78
+ index = parent.children.index(element)
79
+ return false unless index && index > 0
80
+
81
+ parent.children[index - 1].is_a?(Headline)
82
+ end
83
+ private :first_textblock_in_section?
84
+
70
85
  def apply_to_Verbatim(element)
71
86
  content = []
72
87
  element.each_line do |i|
@@ -105,21 +120,21 @@ EOT
105
120
  end
106
121
 
107
122
  def apply_to_DescListItem(element, term, description)
108
- anchor = refer(element)
123
+ refer(element)
109
124
  if description.empty?
110
- ".TP\n.fi\n.B\n#{term.join(" ")}"
125
+ ".TP\n.B\n#{term.join(" ")}"
111
126
  else
112
- %[.TP\n.fi\n.B\n#{term.join(" ")}\n#{description.join("\n")}].chomp
127
+ %[.TP\n.B\n#{term.join(" ")}\n#{description.join("\n")}].chomp
113
128
  end
114
129
  end
115
130
 
116
131
  def apply_to_MethodListItem(element, term, description)
117
132
  term = parse_method(term) # maybe: term -> element.term
118
- anchor = refer(element)
133
+ refer(element)
119
134
  if description.empty?
120
- ".TP\n.fi\n.B\n#{term.join(" ")}"
135
+ ".TP\n.B\n#{term.join(" ")}"
121
136
  else
122
- %[.TP\n.fi\n.B\n#{term.join(" ")}\n#{description.join("\n")}]
137
+ %[.TP\n.B\n#{term.join(" ")}\n#{description.join("\n")}]
123
138
  end
124
139
  end
125
140
 
@@ -181,7 +196,7 @@ EOT
181
196
  # warning?
182
197
  ""
183
198
  else
184
- num = @index[key] = @index.size
199
+ @index[key] = @index.size
185
200
  %{\\&\\fB#{content.join.sub(/\./, '\\.')}\\fP}
186
201
  end
187
202
  end
@@ -230,17 +245,27 @@ EOT
230
245
  private :meta_char_escape
231
246
 
232
247
  def guess_title
233
- return @input_filename unless @input_filename == "-"
234
- return @filename if @filename
235
- "Untitled"
248
+ path = if @input_filename != "-"
249
+ @input_filename
250
+ elsif @filename
251
+ @filename
252
+ end
253
+ return "Untitled" unless path
254
+
255
+ File.basename(path).sub(/\.[^.]+\z/, "")
236
256
  end
237
257
  private :guess_title
238
258
 
259
+ def output_suffix
260
+ @man_section || OUTPUT_SUFFIX
261
+ end
262
+
239
263
 
240
264
  end # RD2MANVisitor
241
265
  end # RD
242
266
 
243
267
  $Visitor_Class = RD::RD2MANVisitor
268
+ $RD2_Sub_OptionParser = "rd/rd2man-opt"
244
269
 
245
270
  =begin
246
271
  == script info.
@@ -0,0 +1,16 @@
1
+ =begin
2
+ = rd2man-opt.rb
3
+ sub-OptionParser for rd2man-lib.rb.
4
+ =end
5
+
6
+ require "optparse"
7
+
8
+ q = $RD2_OptionParser || ARGV.options
9
+
10
+ q.on_tail("rd2man-lib options:")
11
+
12
+ q.on_tail("--man-section=SECTION",
13
+ String,
14
+ "use SECTION as the man page section") do |section|
15
+ $Visitor.man_section = section
16
+ end
@@ -466,7 +466,6 @@ end
466
466
  private :set_term_to_element
467
467
 
468
468
  def on_error( et, ev, _values )
469
- line = @src[@i]
470
469
  prv, cur, nxt = format_line_num(@i, @i+1, @i+2)
471
470
 
472
471
  raise ParseError, <<Msg