# This file is the core of Rassmalog. #-- # Copyright 2006 Suraj N. Kurapati # See the file named LICENSE for details. require 'rake/clean' require 'yaml' require 'time' require 'cgi' require 'ostruct' require 'enumerator' require 'erb' include ERB::Util require 'version' require 'config/format' # utility logic # Wraps the given error inside the given message, while # preserving its original stack trace, and raises it. def raise_error aMessage, aError = $! raise aError.class, "#{aMessage}:\n#{aError}", aError.backtrace end # Notify the user about some action being performed. def notify aAction, aMessage printf "%12s %s\n", aAction, aMessage end # Writes the given content to the given file. def File.write aPath, aContent File.open(aPath, 'w') {|f| f << aContent} end # Returns a hyperlink to the given URL of # the given name and mouse-hover title. def link aUrl, aName = nil, aTitle = nil aName ||= aUrl %{#{aName}} end # Returns HTML for embedding an icon from the input/icons/ directory. def icon aFileName, aAlt = nil, aTitle = nil aTitle ||= aAlt %{#{aAlt}} end # Returns a safe file name that is composed of the # given words and has the given file extension. def make_file_name aExtension, *aWords #:nodoc: aWords.join(' ').to_file_name.ext(aExtension) end class String # Transforms this string into a vaild file name that can be safely used # in a URL. See http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax def to_file_name downcase.strip.gsub(%r{[/;?#[:space:][:punct:]]+}, '-').gsub(/^-|-$/, '') end # 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 32 # ASCII character code for a single space end end.pack('U*').strip.gsub(/[[:space:]-]+/, '-') end # Passes this string through ERB and returns the result. def thru_erb aBinding = Kernel.binding ERB.new(self).result(aBinding) end # Transforms this string into an escaped POSIX shell # argument whilst preserving Unicode characters. def shell_escape inspect.gsub(/\\(\d{3})/) { $1.to_i(8).chr } end @@uriFrags = [] # Resets the list of uri_fragments encountered thus far. def String.reset_uri_fragments #:nodoc: @@uriFrags.clear end # Builds a table of contents from XHTML headings (

,

, etc.) found # in this string and returns an array containing [toc, html] where: # # toc:: the generated table of contents # # html:: a modified version of this string which # contains anchors for the hyperlinks in # the table of contents (so that the TOC # can link to the content in this string) # def table_of_contents toc = '' * prevDepth toc << '' # collapse redundant list elements while toc.gsub! %r{(
  • .*?)
  • (