Class: XHTMLPage

Inherits:
WebPage show all
Defined in:
lib/ribit/webpage.rb

Direct Known Subclasses

CategoriesPage, EditCategoryPage, EditPage, TextPage

Instance Method Summary collapse

Methods inherited from WebPage

#get_content_type, #output_page

Constructor Details

#initialize(xhtmlData) ⇒ XHTMLPage

Throws error if not valid XHTML document



83
84
85
86
87
# File 'lib/ribit/webpage.rb', line 83

def initialize( xhtmlData )
  @doc = XHTMLDocument.new( xhtmlData )
  @logger = RibitLogger.new( XHTMLPage )
  @messageText = nil
end

Instance Method Details

#add_head_element(element) ⇒ Object



126
127
128
# File 'lib/ribit/webpage.rb', line 126

def add_head_element( element )
  @doc.( element )
end

#get_dataObject

Fills the template document and returns document in String format



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ribit/webpage.rb', line 95

def get_data
  
  # TODO: can following be optional or should they be removed
  #      decl = REXML::XMLDecl.new
  #      decl.encoding= REXML::XMLDecl::DEFAULT_ENCODING
  #      @doc << decl
  #      
  #      # write DOCTYPE
  #      info = [
  #      'html',
  #      'PUBLIC',
  #      '"-//W3C//DTD XHTML Basic 1.0//EN"',
  #      '"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"']
  #  
  #      @doc << REXML::DocType.new( info )
  
  @doc.process_marked_elements do |id, ele|
    @logger.debug( "Handling action for ID=#{id}, ele.name=#{ele.name}" )
    begin
      handle_element( id, ele )
    rescue RibitException => e 
      @logger.warn( "Couldn't handle action in HTML, element will be removed: #{e.message}" )
      @logger.warn( format_exception( e ) )
      ele.remove
    end
  end
  
  return @doc.to_s
end

#handle_element(id, element) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ribit/webpage.rb', line 131

def handle_element( id, element )
  case id
  when 'link:main'
    if ( element.name != 'a' )
      raise RibitException, "link:main is not suitable for element #{element.name}", caller
    end
    
    actionEle = LinkActionElement.new( id, element )
    
    # no URL => refers to base URL that points to 
    # the default page (=main page)
    actionEle.url = ''
    
  when 'link:categories'
    if ( element.name != 'a' )
      raise RibitException, "link:categories is not suitable for element #{element.name}", caller
    end
    
    actionEle = LinkActionElement.new( id, element )
    actionEle.url = ViewCategoriesActionAdapter.new().get_url
    @logger.debug( "URL to ViewCategories is " + actionEle.url )
    
  when 'text:message'
    # replaces all 'text:message' entries
    if ( @messageText != nil )
      insert_html_text( element, @messageText )
    else
      element.remove
    end
    
  else
    # rest of text and link elements are removed
    if ( /^text:/.match( id ) or /^link:/.match( id ) )
      element.remove
    else
      raise RibitException, "Unknown actionID=#{id}, don't know how to handle", caller
    end
  end
end

#insert_html_text(element, text) ⇒ Object

insert text that can contain html tags => they are not escaped



172
173
174
175
176
177
178
# File 'lib/ribit/webpage.rb', line 172

def insert_html_text( element, text )
  element.text = nil
  # NOTE: some reason setting raw in constructor didn't work 
  text = REXML::Text.new( text, true, nil, false )
  text.raw = true
  element.add_text( text )
end

#set_message(text) ⇒ Object



90
91
92
# File 'lib/ribit/webpage.rb', line 90

def set_message( text )
  @messageText = text
end