ron 0.1 → 0.3

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/README.md ADDED
@@ -0,0 +1,133 @@
1
+ ron -- the opposite of roff
2
+ ===========================
3
+
4
+ ## DESCRIPTION
5
+
6
+ Ron is a humane text format and toolchain for creating UNIX man
7
+ pages, and things that appear as man pages from a distance. Use it
8
+ to build and install standard UNIX roff man pages or to generate
9
+ nicely formatted HTML manual pages for the web.
10
+
11
+ The Ron file format is based on Markdown. In fact, Ron files are a
12
+ compatible subset of Markdown syntax but have a more rigid structure and
13
+ extend Markdown in some ways to provide features commonly found in man
14
+ pages (e.g., definition lists). The ron(5) manual page defines the
15
+ format in more detail.
16
+
17
+ ## DOCUMENTATION
18
+
19
+ The `.ron` files located under the `man/` directory show off a wide
20
+ range of ron capabilities and are the source of Ron's own documentation.
21
+ The source files and generated HTML / roff output files are available
22
+ at:
23
+
24
+ * [ron(1)](http://rtomayko.github.com/ron/ron.1.html) -
25
+ build markdown based manual pages at the command line.
26
+ [source file](http://github.com/rtomayko/ron/blob/master/man/ron.1.ron),
27
+ [roff output](http://github.com/rtomayko/ron/blob/master/man/ron.1)
28
+
29
+ * [ron(5)](http://rtomayko.github.com/ron/ron.5.html) -
30
+ humane manual page authoring format syntax reference.
31
+ [source file](http://github.com/rtomayko/ron/blob/master/man/ron.5.ron),
32
+ [roff output](http://github.com/rtomayko/ron/blob/master/man/ron.5)
33
+
34
+ * [markdown(5)](http://rtomayko.github.com/ron/markdown.5.html) -
35
+ humane text markup syntax (taken from
36
+ [Markdown Syntax](http://daringfireball.net/projects/markdown/syntax),
37
+ John Gruber)
38
+ [source file](http://github.com/rtomayko/ron/blob/master/man/ron.5.ron),
39
+ [roff output](http://github.com/rtomayko/ron/blob/master/man/ron.5)
40
+
41
+ ## INSTALL
42
+
43
+ Install with Rubygems:
44
+
45
+ $ [sudo] gem install ron
46
+ $ ron --help
47
+
48
+ Or, clone the git repository:
49
+
50
+ $ git clone git://github.com/rtomayko/ron.git
51
+ $ PATH=ron/bin:$PATH
52
+ $ ron --help
53
+
54
+ ## BASIC USAGE
55
+
56
+ To generate a roff man page from the included
57
+ [`markdown.5.ron`](man/markdown.5.ron) file and open it with man(1):
58
+
59
+ $ ron -b man/markdown.5.ron
60
+ building: man/markdown.5
61
+ $ man man/markdown.5
62
+
63
+ To generate a standalone HTML version:
64
+
65
+ $ ron -b --html man/markdown.5.ron
66
+ building: man/markdown.5.html
67
+ $ open man/markdown.5.html
68
+
69
+ To build roff and HTML versions of all ron files:
70
+
71
+ $ ron -b --roff --html man/*.ron
72
+
73
+ If you just want to view a ron file as if it were a man page without
74
+ building intermediate files:
75
+
76
+ $ ron -m man/markdown.5.ron
77
+
78
+ The [ron(1)](http://rtomayko.github.com/ron/ron.1.html) manual page
79
+ includes comprehensive documentation on `ron` command line options.
80
+
81
+ ## ABOUT
82
+
83
+ Some people think UNIX manual pages are a poor and outdated style of
84
+ documentation. I disagree:
85
+
86
+ - Man pages follow a well defined structure that's immediately
87
+ familiar and provides a useful starting point for developers
88
+ documenting new tools, libraries, and formats.
89
+
90
+ - Man pages get to the point. Because they're written in an inverted
91
+ style, with a SYNOPSIS section followed by additional detail,
92
+ prose and references to other sources of information, man pages
93
+ provide the best of both cheat sheet and reference style
94
+ documentation.
95
+
96
+ - Man pages have extremely -- unbelievably -- limited text
97
+ formatting capabilities. You get a couple of headings, lists, bold,
98
+ underline and no more. This is a feature.
99
+
100
+ - Although two levels of section hierarchy are technically
101
+ supported, most man pages use only a single level. Unwieldy
102
+ document hierarchies complicate otherwise good documentation.
103
+ Feynman covered all of physics -- heavenly bodies through QED --
104
+ with only two levels of document hierarchy (_The Feynman Lectures
105
+ on Physics_, 1970).
106
+
107
+ - Man pages have a simple referencing syntax; e.g., sh(1), fork(2),
108
+ markdown(5). HTML versions can use this to generate links between
109
+ pages.
110
+
111
+ - The classical terminal man page display is typographically well
112
+ thought out. Big bold section headings, justified monospace text,
113
+ nicely indented paragraphs, intelligently aligned definition
114
+ lists, and an informational header and footer.
115
+
116
+ Unfortunately, trying to figure out how to create a man page is a
117
+ fairly tedious process. The roff/man macro languages are highly
118
+ extensible, fractured between multiple dialects, and include a bunch
119
+ of device specific stuff that's entirely irrelevant to modern
120
+ publishing tools.
121
+
122
+ Ron aims to address many of the issues with man page creation while
123
+ preserving the things that makes man pages a great form of
124
+ documentation.
125
+
126
+ ## COPYING
127
+
128
+ Ron is Copyright (C) 2009 [Ryan Tomayko](http://tomayko.com/about)
129
+ See the file COPYING for information of licensing and distribution.
130
+
131
+ ## SEE ALSO
132
+
133
+ ron(1), ron(5), markdown(5)
data/Rakefile CHANGED
@@ -1,14 +1,27 @@
1
1
  require 'rake/clean'
2
- require 'rake/testtask'
3
2
 
4
3
  task :default => :test
5
- task :spec => :test
6
4
 
7
- # SPECS ===============================================================
5
+ task :environment do
6
+ require_library 'nokogiri'
7
+ require_library 'rdiscount'
8
+ ENV['RUBYLIB'] = "#{$:.join(':')}:#{ENV['RUBYLIB']}"
9
+ ENV['PATH'] = "bin:#{ENV['PATH']}"
10
+ end
11
+
12
+ desc 'Run tests'
13
+ task :test => :environment do
14
+ require_library 'contest'
15
+ if ENV['PATH'].split(':').any? { |p| File.executable?("#{p}/turn") }
16
+ sh 'turn -Ilib test/*_test.rb'
17
+ else
18
+ sh 'testrb Ilib test/*_test.rb'
19
+ end
20
+ end
8
21
 
9
- Rake::TestTask.new(:test) do |t|
10
- t.test_files = FileList['test/*_test.rb']
11
- t.ruby_opts = ['-rubygems'] if defined? Gem
22
+ desc 'Build the manual'
23
+ task :man => :environment do
24
+ sh "ron -br5 --manual='Ron Manual' --organization='Ryan Tomayko' man/*.ron"
12
25
  end
13
26
 
14
27
  # PACKAGING ============================================================
@@ -71,3 +84,11 @@ file 'ron.gemspec' => FileList['{lib,test}/**','Rakefile'] do |f|
71
84
  File.open(f.name, 'w') { |io| io.write(spec) }
72
85
  puts "updated #{f.name}"
73
86
  end
87
+
88
+ # Misc ===============================================================
89
+
90
+ def require_library(name)
91
+ require name
92
+ rescue LoadError => boom
93
+ abort "fatal: the '#{name}' library is required (gem install #{name})"
94
+ end
data/bin/ron CHANGED
@@ -1,24 +1,38 @@
1
1
  #!/usr/bin/env ruby
2
- ## Usage: ron [ OPTIONS ] [ FILE ... ]
2
+ ## Usage: ron [ OPTIONS ] [ FILE ]
3
3
  ## ron --build FILE ...
4
4
  ## ron --install FILE ...
5
- ## ron --man FILE
6
- ## Convert ron file to roff or html.
5
+ ## ron --man FILE ...
6
+ ## Convert ron FILE to roff man page or HTML and write to standard
7
+ ## output. With no FILE, ron reads from standard input. The build,
8
+ ## install, and man forms accept multiple FILE arguments.
7
9
  ##
8
- ## Options
9
- ## -b, --build write output to files instead of to stdout
10
- ## -i, --install write manpage to MAN_HOME or system man path
11
- ## -m, --man show man page like man(1)
10
+ ## Modes:
11
+ ## --pipe write to standard output (default behavior)
12
+ ## -b, --build write to files instead of standard output
13
+ ## -i, --install write to file in MAN_HOME or system man path
14
+ ## -m, --man open man page like man(1)
12
15
  ##
13
- ## --roff generate roff/man text; this is the default behavior
14
- ## -5, --html generate HTML
16
+ ## Formats:
17
+ ## -r, --roff generate roff/man text; this is the default behavior
18
+ ## -5, --html generate entire HTML page with layout
19
+ ## -f, --fragment generate HTML fragment instead of entire HTML page
15
20
  ##
16
- ## --help show this help message
21
+ ## Document attributes:
22
+ ## --date=DATE published date in YYYY-MM-DD format;
23
+ ## displayed bottom-center in footer
24
+ ## --manual=NAME name of the manual this document belongs to;
25
+ ## displayed top-center in header
26
+ ## --organization=NAME publishing group, organization, or individual;
27
+ ## displayed bottom-left in footer
17
28
  ##
18
- ## See the ron(2)
29
+ ## --help show this help message
30
+ ##
31
+ require 'date'
19
32
  require 'optparse'
20
33
 
21
34
  formats = []
35
+ options = {}
22
36
  build = false
23
37
  install = false
24
38
  man = false
@@ -38,12 +52,22 @@ end
38
52
 
39
53
  # parse command line options
40
54
  ARGV.options do |option|
41
- option.on("--roff") { formats << 'roff' }
42
- option.on("-5", "--html") { formats << 'html' }
55
+ # modes
56
+ option.on("--pipe") { }
43
57
  option.on("-b", "--build") { build = true }
44
58
  option.on("-i", "--install") { install = true }
45
59
  option.on("-m", "--man") { man = true }
46
60
 
61
+ # format options
62
+ option.on("-r", "--roff") { formats << 'roff' }
63
+ option.on("-5", "--html") { formats << 'html' }
64
+ option.on("-f", "--fragment") { formats << 'html_fragment' }
65
+
66
+ # manual attribute options
67
+ [:name, :section, :manual, :organization, :date].each do |option_attr|
68
+ option.on("--#{option_attr}=VALUE") { |val| options[option_attr] = val }
69
+ end
70
+
47
71
  option.on_tail("--help") { usage ; exit }
48
72
  option.parse!
49
73
  end
@@ -55,13 +79,23 @@ elsif ARGV.empty?
55
79
  ARGV.push '-'
56
80
  end
57
81
 
82
+ # turn the --date arg into a real date object
83
+ options[:date] &&= Date.strptime(options[:date], '%Y-%m-%d')
84
+
58
85
  formats = ['roff'] if formats.empty?
86
+ formats.delete('html') if formats.include?('html_fragment')
59
87
  pid = nil
60
88
 
61
- require 'ron'
89
+ begin
90
+ require 'ron'
91
+ rescue LoadError
92
+ $:.unshift File.dirname(__FILE__) + "../lib"
93
+ require 'ron'
94
+ end
95
+
62
96
  wr = STDOUT
63
97
  ARGV.each do |file|
64
- doc = Ron.new(file) { file == '-' ? STDIN.read : File.read(file) }
98
+ doc = Ron.new(file, options) { file == '-' ? STDIN.read : File.read(file) }
65
99
 
66
100
  # setup the man pipeline if the --man option was specified
67
101
  if man && !build
@@ -77,13 +111,15 @@ ARGV.each do |file|
77
111
 
78
112
  # write output for each format
79
113
  formats.each do |format|
80
- output = doc.convert(format)
81
114
  if build
82
- path = doc.path(format)
83
- info "building: #{path}"
84
- File.open(path, 'wb') { |f| f.write(output) }
115
+ path = doc.path_for(format)
116
+ info "building: #{path}" if build
117
+ output = doc.convert(format)
118
+ File.open(path, 'wb') { |f| f.puts(output) }
119
+ system "man #{path}" if man && format == 'roff'
85
120
  else
86
- wr.write(output)
121
+ output = doc.convert(format)
122
+ wr.puts(output)
87
123
  end
88
124
  end
89
125
 
data/lib/ron/document.rb CHANGED
@@ -1,60 +1,178 @@
1
+ require 'set'
1
2
  require 'nokogiri'
2
3
  require 'rdiscount'
3
4
  require 'ron/roff'
4
5
 
5
6
  module Ron
7
+ # The Document class can be used to load and inspect a ron document
8
+ # and to convert a ron document into other formats, like roff or
9
+ # HTML.
10
+ #
11
+ # Ron files may optionally follow the naming convention:
12
+ # "<name>.<section>.ron". The <name> and <section> are used in
13
+ # generated documentation unless overridden by the information
14
+ # extracted from the document's name section.
6
15
  class Document
7
- VERSION = '0.1'
8
- attr_reader :filename, :data, :basename, :name, :section, :tagline
16
+ attr_reader :path, :data
9
17
 
10
- def initialize(filename, &block)
11
- @filename = filename
18
+ # The man pages name: usually a single word name of
19
+ # a program or filename; displayed along with the section in
20
+ # the left and right portions of the header as well as the bottom
21
+ # right section of the footer.
22
+ attr_accessor :name
23
+
24
+ # The man page's section: a string whose first character
25
+ # is numeric; displayed in parenthesis along with the name.
26
+ attr_accessor :section
27
+
28
+ # Single sentence description of the thing being described
29
+ # by this man page; displayed in the NAME section.
30
+ attr_accessor :tagline
31
+
32
+ # The manual this document belongs to; center displayed in
33
+ # the header.
34
+ attr_accessor :manual
35
+
36
+ # The name of the group, organization, or individual responsible
37
+ # for this document; displayed in the left portion of the footer.
38
+ attr_accessor :organization
39
+
40
+ # The date the document was published; center displayed in
41
+ # the document footer.
42
+ attr_accessor :date
43
+
44
+ # Create a Ron::Document given a path or with the data returned by
45
+ # calling the block. The document is loaded and preprocessed before
46
+ # the intialize method returns. The attributes hash may contain values
47
+ # for any writeable attributes defined on this class.
48
+ def initialize(path=nil, attributes={}, &block)
49
+ @path = path
50
+ @basename = path.to_s =~ /^-?$/ ? nil : File.basename(path)
12
51
  @reader = block || Proc.new { |f| File.read(f) }
13
- @data = @reader.call(filename)
52
+ @data = @reader.call(path)
53
+ @name, @section, @tagline = nil
54
+ @manual, @organization, @date = nil
55
+ @fragment = preprocess
56
+ attributes.each { |attr_name,value| send("#{attr_name}=", value) }
57
+ end
14
58
 
15
- @basename = File.basename(filename)
16
- @name, @section =
17
- if @basename =~ /(\w+)\.(\d\w*)\.ron/
18
- [$1, $2]
19
- else
20
- [@basename[/\w+/], nil]
21
- end
59
+ # Generate a file basename of the form "<name>.<section>.<type>"
60
+ # for the given file extension. Uses the name and section from
61
+ # the source file path but falls back on the name and section
62
+ # defined in the document.
63
+ def basename(type=nil)
64
+ type = nil if ['', 'roff'].include?(type.to_s)
65
+ [path_name || @name, path_section || @section, type].
66
+ compact.join('.')
67
+ end
68
+
69
+ # Construct a path for a file near the source file. Uses the
70
+ # Document#basename method to generate the basename part and
71
+ # appends it to the dirname of the source document.
72
+ def path_for(type=nil)
73
+ if @basename
74
+ File.join(File.dirname(path), basename(type))
75
+ else
76
+ basename(type)
77
+ end
22
78
  end
23
79
 
24
- # Construct a path to a file near the input file.
25
- def path(extension=nil)
26
- extension = nil if ['', 'roff'].include?(extension.to_s)
27
- name = "#{@name}.#{section}"
28
- name = "#{name}.#{extension}" if extension
29
- File.join(File.dirname(filename), name)
80
+ # Returns the <name> part of the path, or nil when no path is
81
+ # available. This is used as the manual page name when the
82
+ # file contents do not include a name section.
83
+ def path_name
84
+ @basename[/^[^.]+/] if @basename
30
85
  end
31
86
 
32
- # Convert the document to :roff or :html
87
+ # Returns the <section> part of the path, or nil when
88
+ # no path is available.
89
+ def path_section
90
+ $1 if @basename.to_s =~ /\.(\d\w*)\./
91
+ end
92
+
93
+ # Returns the manual page name based first on the document's
94
+ # contents and then on the path name.
95
+ def name
96
+ @name || path_name
97
+ end
98
+
99
+ # Truthful when the name was extracted from the name section
100
+ # of the document.
101
+ def name?
102
+ !name.nil?
103
+ end
104
+
105
+ # Returns the manual page section based first on the document's
106
+ # contents and then on the path name.
107
+ def section
108
+ @section || path_section
109
+ end
110
+
111
+ # True when the section number was extracted from the name
112
+ # section of the document.
113
+ def section?
114
+ !section.nil?
115
+ end
116
+
117
+ # The date the man page was published. If not set explicitly,
118
+ # this is the file's modified time or, if no file is given,
119
+ # the current time.
120
+ def date
121
+ return @date if @date
122
+ return File.mtime(path) if File.exist?(path)
123
+ Time.now
124
+ end
125
+
126
+ # Convert the document to :roff, :html, or :html_fragment and
127
+ # return the result as a string.
33
128
  def convert(format)
34
129
  send "to_#{format}"
35
130
  end
36
131
 
37
- # Convert the document to roff.
132
+ # Convert the document to roff and return the result as a string.
38
133
  def to_roff
39
134
  RoffFilter.new(
40
135
  to_html_fragment,
41
136
  name,
42
137
  section,
43
- tagline
138
+ tagline,
139
+ manual,
140
+ organization,
141
+ date
44
142
  ).to_s
45
143
  end
46
144
 
47
- # Convert the document to HTML and return result
48
- # as a string.
145
+ # Convert the document to HTML and return the result as a string.
49
146
  def to_html
50
147
  layout_filter(to_html_fragment)
51
148
  end
52
149
 
53
- # Convert the document to HTML and return result
150
+ # Convert the document to HTML and return the result
54
151
  # as a string. The HTML does not include <html>, <head>,
55
152
  # or <style> tags.
56
153
  def to_html_fragment
57
- definition_list_filter(markdown_filter(data))
154
+ buf = []
155
+ if name? && section?
156
+ buf << "<h2 id='NAME'>NAME</h2>"
157
+ buf << "<p><code>#{name}</code> -- #{tagline}</p>"
158
+ elsif tagline
159
+ buf << "<h1>#{[name, tagline].compact.join(' -- ')}</h1>"
160
+ end
161
+ buf << @fragment.to_s
162
+ buf.join("\n")
163
+ end
164
+
165
+ protected
166
+ # Parse the document and extract the name, section, and tagline
167
+ # from its contents. This is called while the object is being
168
+ # initialized.
169
+ def preprocess
170
+ [
171
+ :angle_quote_pre_filter,
172
+ :markdown_filter,
173
+ :angle_quote_post_filter,
174
+ :definition_list_filter
175
+ ].inject(data) { |res,filter| send(filter, res) }
58
176
  end
59
177
 
60
178
  # Apply the standard HTML layout template.
@@ -64,31 +182,12 @@ module Ron
64
182
  eval("%Q{#{template}}", binding, template_file)
65
183
  end
66
184
 
67
- # Run markdown on the data and extract name, section, and
68
- # tagline.
69
- def markdown_filter(data)
70
- html = Markdown.new(data).to_html
71
- @tagline, html = html.split("</h1>\n", 2)
72
- @tagline.sub!('<h1>', '')
73
-
74
- # grab name and section from title
75
- if @tagline =~ /([\w_:-]+)\((\d\w*)\) -- (.*)/
76
- @name, @section = $1, $2
77
- @tagline = $3
78
- end
79
-
80
- "<h2 id='NAME'>NAME</h2>\n" +
81
- "<p><code>#{@name}</code> -- #{@tagline}</p>\n" +
82
- html
83
- end
84
-
85
185
  # Convert special format unordered lists to definition lists.
86
186
  def definition_list_filter(html)
87
- doc = Nokogiri::HTML(html)
88
-
187
+ doc = parse_html(html)
89
188
  # process all unordered lists depth-first
90
- doc.xpath('//ul').to_a.reverse.each do |ul|
91
- items = ul.xpath('li')
189
+ doc.search('ul').to_a.reverse.each do |ul|
190
+ items = ul.search('li')
92
191
  next if items.any? { |item| item.text.split("\n", 2).first !~ /:$/ }
93
192
 
94
193
  ul.name = 'dl'
@@ -103,15 +202,88 @@ module Ron
103
202
  term, definition = container.inner_html.split(":\n", 2)
104
203
 
105
204
  dt = item.before("<dt>#{term}</dt>").previous_sibling
106
- dt['class'] = 'flush' if dt.content.length <= 10
205
+ dt['class'] = 'flush' if dt.content.length <= 7
107
206
 
108
207
  item.name = 'dd'
109
208
  container.swap(wrap.sub(/></, ">#{definition}<"))
110
209
  end
111
210
  end
211
+ doc
212
+ end
112
213
 
113
- doc.css('html > body').inner_html
214
+ # Perform angle quote (<THESE>) post filtering.
215
+ def angle_quote_post_filter(html)
216
+ doc = parse_html(html)
217
+ # convert all angle quote vars nested in code blocks
218
+ # back to the original text
219
+ doc.search('code text()').each do |node|
220
+ next unless node.to_s.include?('var&gt;')
221
+ new = node.document.create_text_node(
222
+ node.to_s.
223
+ gsub('&lt;var&gt;', '<').
224
+ gsub("&lt;/var&gt;", '>')
225
+ )
226
+ node.replace(new)
227
+ end
228
+ doc
114
229
  end
115
230
 
231
+ # Run markdown on the data and extract name, section, and
232
+ # tagline.
233
+ def markdown_filter(data)
234
+ html = Markdown.new(data).to_html
235
+ @tagline, html = html.split("</h1>\n", 2)
236
+ if html.nil?
237
+ html = @tagline
238
+ @tagline = nil
239
+ else
240
+ # grab name and section from title
241
+ @tagline.sub!('<h1>', '')
242
+ if @tagline =~ /([\w_.\[\]~+=@:-]+)\s*\((\d\w*)\)\s*--?\s*(.*)/
243
+ @name = $1
244
+ @section = $2
245
+ @tagline = $3
246
+ elsif @tagline =~ /([\w_.\[\]~+=@:-]+)\s+--\s+(.*)/
247
+ @name = $1
248
+ @tagline = $2
249
+ end
250
+ end
251
+
252
+ html.to_s
253
+ end
254
+
255
+ # Convert all <WORD> to <var>WORD</var> but only if WORD
256
+ # isn't an HTML tag.
257
+ def angle_quote_pre_filter(data)
258
+ data.gsub(/\<([^:.\/]+?)\>/) do |match|
259
+ contents = $1
260
+ tag, attrs = contents.split(' ', 2)
261
+ if attrs =~ /\/=/ ||
262
+ HTML.include?(tag.sub(/^\//, '')) ||
263
+ data.include?("</#{tag}>")
264
+ match.to_s
265
+ else
266
+ "<var>#{contents}</var>"
267
+ end
268
+ end
269
+ end
270
+
271
+ HTML = %w[
272
+ a abbr acronym b bdo big br cite code dfn
273
+ em i img input kbd label q samp select
274
+ small span strong sub sup textarea tt var
275
+ address blockquote div dl fieldset form
276
+ h1 h2 h3 h4 h5 h6 hr noscript ol p pre
277
+ table ul
278
+ ].to_set
279
+
280
+ private
281
+ def parse_html(html)
282
+ if html.kind_of?(Nokogiri::HTML::DocumentFragment)
283
+ html
284
+ else
285
+ Nokogiri::HTML.fragment(html.to_s)
286
+ end
287
+ end
116
288
  end
117
289
  end