# Copyright 2007 Suraj N. Kurapati
# See the file named LICENSE for details.
desc: web page for the Internet
code: |
# load the String#to_html method
require 'gerbil/html'
# load admonition icons
Icon = Struct.new(:path, :name, :format, :data)
class Icon
# Returns a HTML image tag containing embedded image data. The given
# attributes (name => value) are applied to the HTML tag declaration.
def to_html aAttributes = {}
atts = aAttributes.inject('') {|s,(k,v)| s << %( #{k}="#{v}") }
%{}
end
end
ICONS = {}
Dir[File.join(Gerbil[:format_home], 'html.icons', '*.*')].each do |path|
ext = File.extname(path)
name = File.basename(path, ext)
data = [File.read(path)].pack('m')
ICONS[name] = Icon.new(path, name, ext.sub('.', ''), data)
end
class String
# Transforms this UTF-8 string into HTML entities.
def to_html_entities
unpack('U*').map! {|c| "#{c};"}.join
end
# Transforms this string into a valid URI fragment.
# See http://www.nmt.edu/tcc/help/pubs/xhtml/id-type.html
def to_uri_fragment
# remove HTML tags from the input
buf = gsub(/<.*?>/, '')
# The first or only character must be a letter.
buf.insert(0, 'a') unless buf[0,1] =~ /[[:alpha:]]/
# The remaining characters must be letters, digits, hyphens (-),
# underscores (_), colons (:), or periods (.) or Unicode characters
buf.unpack('U*').map! do |code|
if code > 0xFF or code.chr =~ /[[:alnum:]\-_:\.]/
code
else
?\s
end
end.pack('U*').strip.gsub(/[[:space:]-]+/, '-')
end
end
class Node
# Returns the user-defined title for this node's content.
def title
@title ||= args[0]
end
# Returns the user-defined indentifer for this node.
def id
@id ||= args[1]
end
@@frags = []
# Returns a unique URI fragment for this node.
def url
unless defined? @url
frag = (id || title || object_id).to_s.to_uri_fragment
frag << frag.object_id.to_s while @@frags.include? frag # make it unique
@@frags << frag
@url = frag
end
@url
end
# Returns the URI fragment for the location in the table
# of contents / list of figures that points this node.
def list_url
@list_url ||= trace.object_id.to_s.to_uri_fragment
end
end
# Returns a hyperlink to the given URL of
# the given name and mouse-hover title.
def link aUrl, aName = aUrl, aTitle = nil
%{#{aName}}
end
nodes:
##
# Structure
header: &header
toc: false
lof: false
index: false
number: false
silent: true
output: <%= @node.content.to_html %>
header_outside_above: *header
header_inside_above: *header
header_inside_below: *header
header_outside_below: *header
footer: *header
footer_outside_above: *header
footer_inside_above: *header
footer_inside_below: *header
footer_outside_below: *header
abstract:
toc: false
lof: false
index: false
number: false
silent: true
output: |
<%= node = @types['header_inside_above'].first and node.output %>
<% if header = @types['header'].first %>
<%= header.output %>
<% else %>
<%= $logo if defined? $logo %>
<%= $title.to_s.to_html %>
<%=
$authors.map do |(name, url)|
if url
%{#{name}}
else
name
end
end.join(', ')
%>
<%= $date %>
<% end %>
<%= node = @types['header_inside_below'].first and node.output %>
<%= node = @types['header_outside_below'].first and node.output %>
<% if abstract = @types['abstract'].first %>
<%= abstract.output %>
<% end %>
<% # table of contents
builder = lambda do |n|
if @spec['nodes'][n.type]['toc']
entry = '%s%s' % [
(n.index.to_s + ' ' if n.index),
n.list_url,
n.url,
n.title.to_s.to_html
]
nested = n.children.inject('') {|s,c| s << builder[c] }
%{
#{entry}#{ "
#{nested}
" unless nested.empty? }
}
else
''
end
end
toc = @roots.inject('') {|s,n| s << builder[n] }
unless toc.empty?
%>
Contents
<%= toc %>
<%
end
%>
<% # list of figures
enums = {} # type => nodes
@spec['nodes'].each_pair do |name, info|
if info['lof']
nodes = @types[name]
enums[name] = nodes unless nodes.empty?
end
end
lof = enums.sort.map do |(type, nodes)|
nested = nodes.map do |n|
%{