Class: Coradoc::Input::HTML::Plugin::Plateau

Inherits:
Coradoc::Input::HTML::Plugin show all
Defined in:
lib/coradoc/input/html/plugins/plateau.rb

Overview

This plugin enhances documents from the PLATEAU project to extract more data.

Usage: reverse_adoc -rcoradoc/input/html/plugins/plateau

--external-images -u raise --output _out/index.adoc index.html

Constant Summary collapse

IM =
/[A-Z0-9]{1,3}/

Instance Attribute Summary

Attributes inherited from Coradoc::Input::HTML::Plugin

#asciidoc_string, #coradoc_tree, #html_tree

Instance Method Summary collapse

Methods inherited from Coradoc::Input::HTML::Plugin

#html_tree_add_hook_post, #html_tree_add_hook_post_by_css, #html_tree_add_hook_pre, #html_tree_add_hook_pre_by_css, #html_tree_change_properties_by_css, #html_tree_change_tag_name_by_css, #html_tree_preview, #html_tree_process_to_adoc, #html_tree_process_to_coradoc, #html_tree_remove_by_css, #html_tree_replace_with_children_by_css, #html_tree_run_hooks, #initialize, new

Constructor Details

This class inherits a constructor from Coradoc::Input::HTML::Plugin

Instance Method Details

#handle_headers(node, coradoc, state) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
# File 'lib/coradoc/input/html/plugins/plateau.rb', line 117

def handle_headers(node, coradoc, state)
  content = coradoc.content.map(&:content).join

  if %w[toc0 toc_0].any? { |i| coradoc.id&.start_with?(i) }
    # Special content
    case content.strip
    when "はじめに" # Introduction
      coradoc.style = "abstract" # The older version document has ".preface"
      coradoc.level_int = 1
    when "改定の概要" # Revision overview
      coradoc.style = "abstract" # The older version document has ".preface"
      coradoc.level_int = 1
    when "参考文献" # Bibliography
      coradoc.style = "bibliography"
      coradoc.level_int = 1
    when "改訂履歴" # Document history
      coradoc.style = "appendix"
      coradoc.level_int = 1
    when "0 概要" # Overview
      coradoc.style = "abstract" # I'm not sure this is correct
      coradoc.level_int = 1
    when "索引" # Index
      coradoc.style = "index" # I'm not sure this is correct
      coradoc.level_int = 1
    else
      warn "Unknown section #{content.inspect}"
    end
  end

  if node.name == "h1"
    if content.start_with?("Annex")
      coradoc.style = "appendix"
      coradoc.content.first.content.sub!(/\AAnnex [A-Z]/, "")
    end
  end

  # Remove numbers
  coradoc.content.first.content.sub!(/\A(#{IM}\.)*#{IM}[[:space:]]/, "")

  coradoc
end

#handle_headers_h4(node, coradoc, state) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/coradoc/input/html/plugins/plateau.rb', line 159

def handle_headers_h4(node, coradoc, state)
  title = Coradoc.strip_unicode(coradoc.content.first.content)
  case title
  when /\A\(\d+\)(.*)/
    coradoc.level_int = 4
    coradoc.content.first.content = $1.strip
    coradoc
  when /\A\d+\)(.*)/
    coradoc.level_int = 5
    coradoc.content.first.content = $1.strip
    coradoc
  when /\A#{IM}\.#{IM}\.#{IM}\.#{IM}(.*)/
    coradoc.level_int = 4
    coradoc.content.first.content = $1.strip
  else
    if title.empty?
      # Strip instances of faulty empty paragraphs
      nil
    else
      ["// FIXME\n", coradoc]
    end
  end
end

#nameObject



10
11
12
# File 'lib/coradoc/input/html/plugins/plateau.rb', line 10

def name
  "PLATEAU"
end

#postprocess_asciidoc_stringObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/coradoc/input/html/plugins/plateau.rb', line 183

def postprocess_asciidoc_string
  str = self.asciidoc_string

  ### Custom indentation handling
  # If there's a step up, add [none]
  str = str.gsub(%r{\s+//-ENDPT2D\s+//-PT3D\s+}, "\n[none]\n")
  str = str.gsub(%r{\s+//-ENDPT2D\s+//-PT4D\s+}, "\n[none]\n")
  str = str.gsub(%r{\s+//-ENDPT3D\s+//-PT4D\s+}, "\n[none]\n")
  # Collapse blocks of text[2,3]data
  str = str.gsub(%r{\s+//-ENDPT[234]D\s+//-PT[234]D\s+}, "\n\n")
  # In the beginning, add [none]
  str = str.gsub(%r{\s+//-PT[234]D\s+}, "\n\n[none]\n")
  # If following with another list, ensure we readd styling
  str = str.gsub(%r{\s+//-ENDPT[234]D\s+\*}, "\n\n[disc]\n*")
  # Otherwise, clean up
  str = str.gsub(%r{\s+//-ENDPT[234]D\s+}, "\n\n")

  self.asciidoc_string = str
end

#preprocess_html_treeObject



14
15
16
17
18
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/coradoc/input/html/plugins/plateau.rb', line 14

def preprocess_html_tree
  # Let's simplify the tree by removing what's extraneous
  # html_tree_remove_by_css("script, style, img.container_imagebox:not([src])")
  # html_tree_replace_with_children_by_css("div.container_box")
  # html_tree_replace_with_children_by_css("div.col.col-12")
  # html_tree_replace_with_children_by_css(".tabledatatext, .tabledatatextY")
  # html_tree_replace_with_children_by_css("div.row")
  #
  # We can remove that, but it messes up the images and paragraphs.

  # Remove side menu, so we can generate TOC ourselves
  html_tree_remove_by_css(".sideMenu")

  # Correct non-semantic classes into semantic HTML tags
  html_tree_change_tag_name_by_css(".titledata", "h1")
  html_tree_change_tag_name_by_css(".subtitledata", "h2")
  html_tree_change_tag_name_by_css(".pitemdata", "h3")
  html_tree_change_tag_name_by_css(".sitemdata", "h4")
  html_tree_change_tag_name_by_css('td[bgcolor="#D0CECE"]', "th")
  html_tree_change_tag_name_by_css('td[bgcolor="#d0cece"]', "th")
  html_tree_change_tag_name_by_css('.framedata, .frame_container_box', 'aside')
  html_tree_change_tag_name_by_css('.frame2data', 'pre')
  # Assumption that all code snippets in those documents are XML...
  html_tree_change_properties_by_css(".frame2data", class: "brush:xml;")

  # Remove some CSS ids that are not important to us
  html_tree_change_properties_by_css("#__nuxt", id: nil)
  html_tree_change_properties_by_css("#__layout", id: nil)
  html_tree_change_properties_by_css("#app", id: nil)

  # Handle lists of document 02
  html_tree_replace_with_children_by_css(".list_num-wrap")

  # Convert table/img caption to become a caption
  html_tree.css(".imagedata").each do |e|
    table = e.parent.next&.children&.first
    if table&.name == "table"
      e.name = "caption"
      table.prepend_child(e)
      next
    end

    img = e.parent.previous&.children&.first
    if img&.name == "img" && img["src"]
      title = e.text.strip
      img["title"] = title
      e.remove
      next
    end
  end

  # Add hooks for H1, H2, H3, H4
  html_tree_add_hook_post_by_css("h1, h2, h3", &method(:handle_headers))
  html_tree_add_hook_post_by_css("h4", &method(:handle_headers_h4))

  # Table cells aligned to center
  html_tree_change_properties_by_css(".tableTopCenter", align: "center")

  # Handle non-semantic lists and indentation
  html_tree_add_hook_pre_by_css ".text2data" do |node,|
    text = html_tree_process_to_adoc(node).strip
    next "" if text.empty? || text == "\u3000"

    if text.start_with?(/\d+\./)
      text = text.sub(/\A\d+.\s*/, "")
      ".. #{text}\n"
    else
      text = text.gsub(/^/, "** ")
      "\n\n//-PT2D\n#{text}\n//-ENDPT2D\n\n"
    end
  end

  (3..4).each do |i|
    html_tree_add_hook_pre_by_css ".text#{i}data" do |node,|
      text = html_tree_process_to_adoc(node).strip
      next "" if text.empty? || text == "\u3000"

      text = text.strip.gsub(/^/, "#{'*' * i} ")
      "\n\n//-PT#{i}D\n#{text}\n//-ENDPT#{i}D\n\n"
    end
  end

  (2..3).each do |i|
    html_tree_add_hook_pre_by_css ".text#{i}data_point ul" do |node,|
      text = html_tree_process_to_adoc(node.children.first.children).strip

      "#{'*' * i} #{text}\n"
    end
  end

  (1..20).each do |i|
    html_tree_add_hook_pre_by_css ".numtextdata_num .list_num#{i}" do |node,|
      text = html_tree_process_to_adoc(node).strip

      "[start=#{i}]\n. #{text}\n"
    end
  end

  # html_tree_preview
end