class DxSectionX

Attributes

domain[W]
dx[R]
xsl_url[W]

Public Class Methods

new(x, domain: nil, xsl_url: nil, debug: false, autosave: false, order: :ascending) click to toggle source
# File lib/dxsectionx.rb, line 17
def initialize(x, domain: nil, xsl_url: nil, debug: false, 
               autosave: false, order: :ascending)

  @domain, @xsl_url, @debug = domain, xsl_url, debug
  
  if x.is_a? Rexle then
    @doc = x
    transform()
  else
    
    @dx = Dynarex.new x, autosave: autosave, debug: debug, order: order
    #jr19-04-2020 @dx.import x
    puts '@dx.to_s : ' + @dx.to_s if @debug

    transform()

  end

end

Public Instance Methods

create(h) click to toggle source
# File lib/dxsectionx.rb, line 37
def create(h)
  puts 'inside dxsection#create' if @debug
  @dx.create h
end
save(filename) click to toggle source
# File lib/dxsectionx.rb, line 42
def save(filename)
  puts 'inside dxsection save ' + filename.inspect if @debug
  @dx.save filename
end
to_doc() click to toggle source
# File lib/dxsectionx.rb, line 47
def to_doc()
  transform()
  @doc
end
to_dx() click to toggle source
# File lib/dxsectionx.rb, line 52
def to_dx()
  @dx.clone
end
to_s() click to toggle source
# File lib/dxsectionx.rb, line 56
def to_s()
  @dx.to_s
end
transform() click to toggle source
# File lib/dxsectionx.rb, line 60
def transform
  
  doc = @dx.doc.clone
  
  doc.root.xpath('records/section/x') do |x|
 
    s = "=%s\n%s\n=" % [x.text.lines.first[/#\w+$/], x.text.unescape]
    puts ('s: ' + s.inspect).debug if @debug
    
    html = Kramdown::Document\
        .new(Martile.new(s, ignore_domainlabel: @domain).to_s).to_html
    
    puts 'html: ' + html.inspect if @debug
    
    e = x.parent
    e.attributes.merge x.attributes
    x.delete
    doc2 = Rexle.new(html)

    h1 = doc2.root.element('h1')
    details = Rexle::Element.new('details')
    details.attributes[:open] = 'open'
    summary = Rexle::Element.new('summary')
    summary.add h1

    details.add summary
    doc2.root.xpath('*').each {|x| details.add x }     
    doc2.root.add details
    
    doc2.root.elements.each {|e2|  e.add e2 }

  end
  @doc = doc

  if @xsl_url then

    @doc.instructions << [
      'xml-stylesheet',
      "title='XSL_formatting' type='text/xsl' href='#{@xsl_url}'"
    ]

  end
  
  return @doc

end