1
# html.rb: html output for internal documentation
2
# copyright (c) 2009 by Vincent Fourmond
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# General Public License for more details (in the COPYING file).
14
require 'ctioga2/utils'
15
require 'ctioga2/commands/commands'
19
Version::register_svn_info('$Revision: 216 $', '$Date: 2010-12-31 16:18:17 +0100 (Fri, 31 Dec 2010) $')
25
# Generation of XHTML snippets (not full pages) that document
26
# the commands/groups and types known to CTioga2.
29
# The Doc object the HTML class should document
32
# The base URL for the file where the types are documented.
33
attr_accessor :types_url
35
# The base URL for the file where the backends are documented.
37
# \todo maybe this should be turned into a directory, and each
38
# file would document a backend on its own ? That would make
39
# sense, but that would be rather difficult, I have to admit.
40
attr_accessor :backends_url
42
# The base URL for file where commands and groups are
44
attr_accessor :commands_url
48
@types_url = "types.html"
49
@commands_url = "commands.html"
50
@backends_url = "backends.html"
53
# Ouputs HTML code to document all groups and commands
54
def write_commands(out = STDOUT)
55
cmds, groups = @doc.documented_commands
57
out.puts "<div class='quick-jump'>"
58
out.puts "Quick jump to a specific group of commands:\n"
61
out.puts "<li><a href='#group-#{g.id}'>#{g.name}</a></li>\n"
68
out.puts "<h3 class='group' id='group-#{g.id}'>#{g.name}</h3>"
69
out.puts markup_to_html(g.description)
71
commands = cmds[g].sort {|a,b|
76
out.puts "<span class='bold'>Available commands:</span>\n"
77
out.puts commands.map {|c|
78
"<a href='#command-#{c.name}'><code>#{c.name}</code></a>"
84
out.puts command_documentation(cmd)
89
# Write a HTML table documenting all command-line options.
90
def write_command_line_options(out = STDOUT)
91
cmds, groups = @doc.documented_commands
95
out.puts "<tr><th colspan='3'>#{g.name}</th></tr>"
96
commands = cmds[g].sort {|a,b|
97
a.long_option <=> b.long_option
100
opts = cmd.option_strings
101
link = "<a href='#{@commands_url}#command-#{cmd.name}'>"
102
out.puts "<tr><td><code>#{link}#{opts[0]}</a></code></td>"
103
out.puts "<td><code>#{link}#{opts[1]}</a></code></td>"
104
out.puts "<td>#{opts[2]}</td></tr>"
112
# Ouputs HTML code to document all types
113
def write_types(out = STDOUT)
114
types = @doc.types.sort.map { |d| d[1]}
117
out.puts "<div class='quick-jump'>"
118
out.puts "Quick jump to a specific type:\n"
121
out.puts "<li><a href='#type-#{t.name}'>#{t.name}</a></li>\n"
128
out.puts "<h4 id='type-#{t.name}' class='type'>#{t.name}</h4>\n"
129
out.puts markup_to_html(t.description)
130
out.puts # There is no need to wrap the markup
136
# Ouputs HTML code to all backends
137
def write_backends(out = STDOUT)
138
backends = @doc.backends.sort.map { |d| d[1]}
141
out.puts "<div class='quick-jump'>"
142
out.puts "Quick jump to a specific backend:\n"
145
out.puts "<li><a href='#backend-#{b.name}'>#{b.name}</a></li>\n"
152
out.puts "<h3 id='backend-#{b.name}' class='backend'><code>#{b.name}</code>: #{b.long_name}</h3>\n"
153
out.puts markup_to_html(b.description)
155
for param in b.param_list
156
out.puts "<h4 id='backend-#{b.name}-#{param.name}'>Parameter: #{param.name}</h4>"
157
out.puts "<p><code>/#{param.name}=<a href='#{@types_url}#type-#{param.type.name}'>#{param.type.name}</a></p>"
158
out.puts markup_to_html(param.description)
166
# The string that represents a full command
167
def command_documentation(cmd)
168
str = "<h4 class='command' id='command-#{cmd.name}'>Command: <code>#{cmd.name}</code></h4>\n"
169
str << "<p class='synopsis'>\n<span class='bold'>Synopsis (file)</span>\n"
171
str << "</p>\n<pre class='examples-cmdfile'>"
172
str << "<span class='cmd'>#{cmd.name}("
173
str << cmd.arguments.map { |arg|
174
"<a class='argument' href='#{@types_url}#type-#{arg.type.name}'>#{arg.displayed_name}</a>"
177
if(cmd.arguments.size > 0)
185
# Command-line file synopsis
186
str << "<p class='synopsis'>\n<span class='bold'>Synopsis (command-line)</span>\n"
187
args = cmd.arguments.map { |arg|
188
"<a class='argument' href='#{@types_url}#type-#{arg.type.name}'>#{arg.displayed_name.upcase}</a>"
191
args << " /option=..."
193
str << "</p>\n<pre class='examples-cmdline'>"
195
str << "<span class='cmdline'>-#{cmd.short_option} "
199
str << "<span class='cmdline'>--#{cmd.long_option} "
205
str << "<p class='synopsis'><span class='bold'>Available options</span>:\n"
206
opts = cmd.optional_arguments.sort.map do |k,arg|
207
"<a href='#{@types_url}#type-#{arg.type.name}'><code>#{k}</code></a>\n"
209
str << opts.join(' ')
212
# Now, the description:
213
str << markup_to_html(cmd.long_description)
217
# Takes up an array of MarkupItem objects and returns its
218
# equivalent in HTML format. Alternativelely, it can take a
219
# String and feed it to MarkedUpText.
221
# \todo escape correctly the produced HTML code...
222
def markup_to_html(items)
223
if items.is_a? String
224
mup = MarkedUpText.new(@doc, items)
225
return markup_to_html(mup.elements)
230
when MarkedUpText::MarkupText
243
str << "#{prefix}#{it.to_s}#{suffix}"
244
when MarkedUpText::MarkupLink
247
link = "#{@commands_url}#command-#{it.target.name}"
249
link = "#{@commands_url}#group-#{it.target.id}"
251
link = "#{@types_url}#type-#{it.target.name}"
252
when Data::Backends::BackendDescription
253
link = "#{@backends_url}#backend-#{it.target.name}"
254
when String # plain URL target
255
link = "#{it.target}"
257
raise "The link target should be either a group, a command or a type, but is a #{it.target.class}"
259
str << "<a href='#{link}'>#{it.to_s}</a>"
260
when MarkedUpText::MarkupItemize
263
str << "<li>#{markup_to_html(x)}</li>\n"
266
when MarkedUpText::MarkupParagraph
267
str << "<p>\n#{markup_to_html(it.elements)}\n</p>\n"
268
when MarkedUpText::MarkupVerbatim
269
str << "<pre class='#{it.cls}'>#{it.text}</pre>\n"
271
raise "Markup #{it.class} isn't implemented yet for HTML"