Module: VivlioStarter::CLI::PostProcessCommands::HtmlParser
- Defined in:
- lib/vivlio_starter/cli/post_process/html_parser.rb
Overview
HTML 解析ユーティリティモジュール
Class Method Summary collapse
-
.parse_html_document(html) ⇒ Object
Nokogiri を用いて HTML 文字列からドキュメントオブジェクトを生成する.
-
.render_html_document(doc) ⇒ Object
Nokogiri ドキュメントを HTML 文字列へ戻す(HTML5/HTML 両対応).
-
.save_html_document(path, doc) ⇒ Object
HTMLドキュメントをファイルに保存.
-
.zero_width_text?(text) ⇒ Boolean
ゼロ幅スペース等のみで構成されているかを判定する.
Class Method Details
.parse_html_document(html) ⇒ Object
Nokogiri を用いて HTML 文字列からドキュメントオブジェクトを生成する
29 30 31 32 33 34 35 |
# File 'lib/vivlio_starter/cli/post_process/html_parser.rb', line 29 def parse_html_document(html) if defined?(Nokogiri::HTML5) Nokogiri::HTML5.parse(html) else Nokogiri::HTML.parse(html, nil, 'UTF-8') end end |
.render_html_document(doc) ⇒ Object
Nokogiri ドキュメントを HTML 文字列へ戻す(HTML5/HTML 両対応)
38 39 40 |
# File 'lib/vivlio_starter/cli/post_process/html_parser.rb', line 38 def render_html_document(doc) doc.respond_to?(:to_html) ? doc.to_html(encoding: 'UTF-8') : doc.to_s end |
.save_html_document(path, doc) ⇒ Object
HTMLドキュメントをファイルに保存
43 44 45 |
# File 'lib/vivlio_starter/cli/post_process/html_parser.rb', line 43 def save_html_document(path, doc) File.write(path, render_html_document(doc), encoding: 'utf-8') end |
.zero_width_text?(text) ⇒ Boolean
ゼロ幅スペース等のみで構成されているかを判定する
48 49 50 |
# File 'lib/vivlio_starter/cli/post_process/html_parser.rb', line 48 def zero_width_text?(text) text.gsub(/[\u200B\u200C\u200D\u2060\uFEFF\u180E]/, '').strip.empty? end |