lib/ctioga2/commands/doc/markup.rb in ctioga2-0.0 vs lib/ctioga2/commands/doc/markup.rb in ctioga2-0.1

- old
+ new

@@ -14,18 +14,22 @@ require 'ctioga2/utils' require 'ctioga2/log' module CTioga2 - Version::register_svn_info('$Revision: 84 $', '$Date: 2009-06-12 00:09:41 +0200 (Fri, 12 Jun 2009) $') + Version::register_svn_info('$Revision: 216 $', '$Date: 2010-12-31 16:18:17 +0100 (Fri, 31 Dec 2010) $') module Commands module Documentation # The documentation strings are written in a simple markup - # language. + # language. + # + # \todo we should provide tags to specifically mark TODO items + # in documentation, in such a way that it would be easy to make + # a list of them, and possibly ignore it for output. class MarkedUpText # Do we really need logging ? include Log @@ -46,20 +50,22 @@ end end # A markup item representing plain text. - # - # TODO: in to_s a simple word-wrapping algorithm could be - # used. class MarkupText < MarkupItem # The text attr_accessor :text - def initialize(doc, text = "", strip = true) + # The kind of markup, nil means no markup + attr_accessor :kind + + def initialize(doc, text = "", strip = true, + kind = nil) super(doc) @text = text + @kind = kind if strip @text.gsub!(/\n/, " ") end end @@ -102,19 +108,27 @@ class MarkupLink < MarkupItem # The object target of the link attr_accessor :target # _target_ is the name of the target, which can be of _type_ - # 'group', 'command' and 'type'. + # 'group', 'command', 'backend', 'type' and 'url' def initialize(doc, target, type) super(doc) - @target = doc.send("#{type}s")[target] + if type =~ /url/ + @target = target + else + @target = doc.send("#{type}s")[target] + end end def to_s - if @target - return @target.name + if @target + begin + return @target.name + rescue NoMethodError + return @target + end else return "unknown" end end @@ -213,10 +227,13 @@ # itemize. The itemize finishes when a new paragraph is # starting. # * a {group: ...} or {type: ...} or {command: ...} is a link # to the element. # * a blank line marks a paragraph break. + # + # \todo Add elements to do some inline markup (such as bold, + # code, italics; mostly code for now will do very fine) def parse_from_string(string) @last_type = nil @last_string = "" lines = string.split(/[ \t]*\n/) @@ -261,18 +278,28 @@ flush_element end protected + # A few constants to help writing out the paragraph markup + LinkRE = /\{(group|type|command|backend|url):\s*([^}]+?)\s*\}/ + + MarkOnceRE = /@([^@]+)@/ + + # Parses the markup found within a paragraph (ie: links and # other text attributes, but not verbatim, list or other # markings) and returns an array containing the MarkupItem # elements. def parse_paragraph_markup(doc, string) els = [] - while string =~ /\{(group|type|command):\s*([^}]+?)\s*\}/ + while string =~ /#{LinkRE}|#{MarkOnceRE}/ els << MarkupText.new(doc, $`) - els << MarkupLink.new(doc, $2, $1) + if $1 + els << MarkupLink.new(doc, $2, $1) + elsif $3 + els << MarkupText.new(doc, $3, true, :code) + end string = $' end els << MarkupText.new(doc, string) return els end