class JsTreeBuilder

Constants

PLAIN_CSS
PLAIN_JS
TREE_CSS
TREE_JS

Attributes

css[R]
html[R]
js[R]

Public Class Methods

new(unknown=nil, options={}) click to toggle source
# File lib/jstreebuilder.rb, line 327
def initialize(unknown=nil, options={})
  
  if unknown.is_a? String or unknown.is_a? Symbol then
    type = unknown.to_sym
  elsif unknown.is_a? Hash
    options = {type: :tree}.merge(unknown)
    type = options[:type]
  end
  
  @debug = options[:debug]

  @types = %i(tree sidebar plain)
  
  build(type, options) if type

end

Public Instance Methods

to_css() click to toggle source
# File lib/jstreebuilder.rb, line 344
def to_css()
  @css
end
to_html() click to toggle source
# File lib/jstreebuilder.rb, line 348
def to_html()
  @html
end
to_js() click to toggle source
# File lib/jstreebuilder.rb, line 352
def to_js()
  @js
end
to_ul() click to toggle source
# File lib/jstreebuilder.rb, line 356
def to_ul()
  @ul
end
to_webpage() click to toggle source
# File lib/jstreebuilder.rb, line 360
def to_webpage()

  a = RexleBuilder.build do |xml|
    xml.html do 
      xml.head do
        xml.meta name: "viewport", content: \
            "width=device-width, initial-scale=1"
        xml.style "\nbody {font-family: Arial;}\n\n" + @css
      end
      xml.body @ul
    end
  end

  doc = Rexle.new(a)    
  
  doc.root.element('body').add \
      Rexle::Element.new('script').add_text "\n" + 
      @js.gsub(/^ +\/\/[^\n]+\n/,'')
  
  "<!DOCTYPE html>\n" + doc.xml(pretty: true, declaration: false)\
      .gsub(/<\/div>/,'\0' + "\n").gsub(/\n *<!--[^>]+>/,'')
  
end
to_xml() click to toggle source
# File lib/jstreebuilder.rb, line 384
def to_xml()
  @xml
end

Private Instance Methods

build(type, options) click to toggle source
# File lib/jstreebuilder.rb, line 391
def build(type, options)
  
  puts 'inside build'.info if @debug
  puts "type: %s\noptions: %s".debug % [type, options] if @debug    
  
  return unless @types.include? type.to_sym
  
  s = method(type.to_sym).call(options)
  
  @html = s.gsub(/<\/div>/,'\0' + "\n").strip.lines[1..-2]\
    .map {|x| x.sub(/^  /,'') }.join
  
  @css = type.to_s.upcase + '_CSS'
  @js = type.to_s.upcase + '_JS'        

end
build_px(tree) click to toggle source
# File lib/jstreebuilder.rb, line 408
def build_px(tree)
  
  schema = 'entries/link[title, url]'
  xslt_schema = 'tree/item[@title:title, @url:url]'

  # transform the tree xml into a polyrex document
  pxsl = PolyrexXSLT.new(schema: schema, xslt_schema: xslt_schema).to_xslt
  puts 'pxsl: ' + pxsl if @debug
  Rexslt.new(pxsl, tree).to_s    
  
end
plain(opt={}) click to toggle source
# File lib/jstreebuilder.rb, line 470
def plain(opt={})
  tree opt, PLAIN
end
sidebar(opt={}) click to toggle source
tree(opt={}, xslt=XSLT) click to toggle source
# File lib/jstreebuilder.rb, line 421
def tree(opt={}, xslt=XSLT)

  raw_src = opt[:src]
  
  px = if raw_src.is_a? String then
  
    src, _ = RXFHelper.read raw_src
    
    header = "<?polyrex schema='entries[title]/link[title,url]' \
          delimiter=' # '?>\n\n"
    
    s = if src =~ /<tree>/ then
      
      build_px(src)
      
    elsif src =~ /<\?polyrex-links\?>/ 
      header + src.sub(/<\?polyrex-links\?>/,'').lstrip
    elsif src =~ /<\?polyrex / 
      src      
    elsif src =~ /^#+/
      build_px(TreeBuilder.new(src, hn: opt[:hn],debug: @debug).to_tree)
    else
      header + src.lstrip
    end
    
    puts ('s: ' + s.inspect).debug if @debug
    Polyrex.new(s)
    
  elsif raw_src.is_a?(Polyrex) # detects PolyrexLinks as Polyrex too
    raw_src
  end
  
  # transform the polyrex xml into a nested HTML list
  #@ul = Rexslt.new(px.to_xml, XSLT).to_xml
  puts ('px: ' + px.inspect).debug if @debug
  puts ('px.to_xml: ' + px.to_xml.inspect).debug if @debug
  doc   = Nokogiri::XML(px.to_xml)
  xslt  = Nokogiri::XSLT(xslt)

  @ul = xslt.transform(doc).to_s.lines[1..-1].join

end