Module: ManipulateNode

Defined in:
public/app/controllers/concerns/manipulate_node.rb

Instance Method Summary collapse

Instance Method Details

#inheritance(struct = nil) ⇒ Object

provides inheritance information, with markup!

[View source]

74
75
76
77
78
79
80
81
# File 'public/app/controllers/concerns/manipulate_node.rb', line 74

def inheritance(struct = nil)
  text = ''
  unless struct.blank? || struct['level'].blank? || struct['direct']
    level = I18n.t("inherit.#{struct['level'].downcase}", :default => struct['level'])
    text = '<span class="inherit">' + I18n.t('inherit.inherited', :level => level) + '</span>'
  end
  text
end

#process_mixed_content(in_txt, opts = {}) ⇒ Object

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'public/app/controllers/concerns/manipulate_node.rb', line 19

def process_mixed_content(in_txt, opts = {})
  return if !in_txt

  # Don't fire up nokogiri if there's no mixed content to parse
  needs_nokogiri = in_txt.include?("<")

  txt = in_txt.strip.encode(
    Encoding.find('utf-8'), { invalid: :replace, undef: :replace, replace: '' }
  )

  txt = txt.gsub("chronlist>", "ul>")
    .gsub("chronitem>", "li>")
  txt = txt.gsub("list>", "ul>")
    .gsub("item>", "li>")

  unless opts[:preserve_newlines]
    txt = txt.gsub(/\n\n/, "<br /><br />")
            .gsub(/\r\n\r\n/, "<br /><br />")
  end

  txt = txt.gsub(/&(?![A-Za-z]+;|#[0-9]+;)/, '&amp;')

  txt = txt.gsub("xlink\:type=\"simple\"", "")

  unless needs_nokogiri
    return txt
  end

  @frag = Nokogiri::XML.fragment(txt)
  move_list_heads
  @frag.traverse { |el|
    # we don't do anything at the top level of the fragment or if it's text
    node_check(el) if el.parent && !el.text?
    el.content = el.text.gsub("\"", "&quot;") if el.text?
  }
  # replace the inline quotes with &quot;
  @frag.to_xml(encoding: 'utf-8').to_s.gsub("&amp;quot;", "&quot;")
end

#process_mixed_content_title(text) ⇒ Object

the beginning of processing mixed content nodes for titles, notes, etc. TODO: look at replacing these gsubs with syntax like: @xml.xpath(“//item”).each do |item| item.name = “li” end

[View source]

13
14
15
16
17
# File 'public/app/controllers/concerns/manipulate_node.rb', line 13

def process_mixed_content_title(text)
  return '' if !text

  Nokogiri::HTML::DocumentFragment.parse(text).to_html
end

#strip_mixed_content(in_text) ⇒ Object

strips all xml markup; used for things like titles.

[View source]

59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'public/app/controllers/concerns/manipulate_node.rb', line 59

def strip_mixed_content(in_text)
  return if !in_text

  # Don't fire up nokogiri if there's no mixed content to parse
  unless in_text.include?("<")
    return in_text
  end

  in_text = in_text.gsub(/ & /, ' &amp; ')
  @frag = Nokogiri::XML.fragment(in_text)

  @frag.content
end