Class: OWLScribble

Inherits:
TagTreeScanner
  • Object
show all
Defined in:
lib/owlscribble.rb

Overview

OWLScribble converts a specific set of text markup into HTML. (The syntax used in the markup is a knockoff of the markup used by OpenWiki, so the ‘OWL’ in OWLScribble means “OpenWiki-like”.)

See the README.txt file for more information.

For details on the markup used by OWLScribble, please see the ../examples/markup.html file.

Constant Summary collapse

VERSION =
"0.9.1"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owl_string) ⇒ OWLScribble

owl_string is the raw OWLScribble markup to convert.

You likely want to use OWLScribble.each_wiki_link and (possibly) OWLScribble.each_wiki_command to define how to produce wiki-specific markup, prior to creating OWLScribble instances.



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/owlscribble.rb', line 506

def initialize( owl_string )
  @list_items = []
  @headers = []
  @wiki_commands = []
  @wiki_links = []
      
  super

  reparent_lists( @list_items ) unless @list_items.empty?
  #TODO - process indented paragraphs like lists, just to calculate minimal depth from many spaces
  add_sections
  
  # Must do this after add_sections has been called
  @wiki_commands.each{ |command_tag|
    case command_tag.attributes[ :do ]
      when 'TableOfContents'
        command_tag.replace_with( @table_of_contents.dup )
    end
  }
end

Class Attribute Details

.handle_wiki_commandObject

Returns the value of attribute handle_wiki_command.



435
436
437
# File 'lib/owlscribble.rb', line 435

def handle_wiki_command
  @handle_wiki_command
end

Returns the value of attribute handle_wiki_link.



435
436
437
# File 'lib/owlscribble.rb', line 435

def handle_wiki_link
  @handle_wiki_link
end

Instance Attribute Details

#headersObject (readonly)

:nodoc:



21
22
23
# File 'lib/owlscribble.rb', line 21

def headers
  @headers
end

#list_itemsObject (readonly)

:nodoc:



20
21
22
# File 'lib/owlscribble.rb', line 20

def list_items
  @list_items
end

#table_of_contentsObject (readonly)

A nested unordered list representing the hierarchy of the document, with links to the sections. (See “Table of Contents” above.)



33
34
35
# File 'lib/owlscribble.rb', line 33

def table_of_contents
  @table_of_contents
end

#wiki_commandsObject (readonly)

An array of <wiki_command> tags representing directives to the wiki. (See “Wiki Commands” above.)



25
26
27
# File 'lib/owlscribble.rb', line 25

def wiki_commands
  @wiki_commands
end

An array of <wiki_link> tags repesenting links to wiki pages. (See “Wiki Links” above.)



29
30
31
# File 'lib/owlscribble.rb', line 29

def wiki_links
  @wiki_links
end

Class Method Details

.each_wiki_command(&block) ⇒ Object

Define how to handle each wiki command instead of using the default handler. The block you supply will be passed a TagTreeScanner::Tag instance, the name of the command, and an array of zero or more parameter strings.

The Tag defaults to <wiki_command>###command( param1, param2 )##</wiki_command>.

Example usage:

OWLScribble.each_wiki_command do |tag, command, params|
  case command

    # Format is Include( page_name )
    # or        Include( page_name, revision )
    when 'Include'
      tag.name  = 'div'
      tag.class = 'sub_page'
      page_name, revision = params
      # All elements in the params array are strings
      revision = Integer( revision ) rescue nil
      tag.text = fetch_page( page_name, revision )

    when 'TitleIndex'
      # ...set tag.html here

    else
      tag.name  = 'span'
      tag.class = 'unhandled_command'
      tag.text = "###{command}( #{params.join ', '} )##"
  end
end


498
499
500
# File 'lib/owlscribble.rb', line 498

def self.each_wiki_command( &block )
  @handle_wiki_command = block
end

Define how to handle each wiki link instead of using the default handler. The block you supply will be passed a TagTreeScanner::Tag instance, the name of the wiki page to link to, and the text to display for the link.

The Tag defaults to <wiki_link>link text</wiki_link>, without the page name.

Example usage:

OWLScribble.each_wiki_link do |tag, page_name, link_text|
  if my_application.page_exists?( page_name )
    tag.name  = "a" #=> html anchor
    tag.href  = "page/view/#{CGI.escape(page_name)}"
    tag.title = "View #{page_name.dewikiword}"
    # tag.text is set to the page_name.dewikiword already

  elsif my_application.user_can_create_pages?
    tag.name  = "a" #=> html anchor
    tag.href  = "page/create/#{CGI.escape(page_name)}"
    tag.title = "Create #{page_name.dewikiword}"
    tag.text  = page_name  # no dewikiword

  else
    tag.name  = "span"
    tag.class = "missing_page"
    tag.text  = page_name      # no dewikiword
  end
end


465
466
467
# File 'lib/owlscribble.rb', line 465

def self.each_wiki_link( &block )
  @handle_wiki_link = block
end

Instance Method Details

#process_wiki_command(tag, command, params) ⇒ Object

This is not called for ##TableOfContents##, since we know how to handle that natively, and it must be handled after the full document hierarchy is created.



535
536
537
538
539
540
541
542
# File 'lib/owlscribble.rb', line 535

def process_wiki_command( tag, command, params ) #:nodoc:
  if !params || params.empty?
    tag.text = "###{command}##"
  else
    tag.text = "###{command}( #{params.join(', ')} )##"
  end
  self.class.handle_wiki_command[ tag, command, params ]
end

:nodoc:



527
528
529
530
# File 'lib/owlscribble.rb', line 527

def process_wiki_link( tag, page_name, link_text ) #:nodoc:
  tag.text = link_text
  self.class.handle_wiki_link[ tag, page_name, link_text ]
end