Module: Vivlio::Starter::CLI::PostProcessCommands::HtmlParser

Defined in:
lib/vivlio/starter/cli/post_process/html_parser.rb

Overview

HTML 解析ユーティリティモジュール

Class Method Summary collapse

Class Method Details

.parse_html_document(html) ⇒ Object

Nokogiri を用いて HTML 文字列からドキュメントオブジェクトを生成する



30
31
32
33
34
35
36
# File 'lib/vivlio/starter/cli/post_process/html_parser.rb', line 30

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 両対応)



39
40
41
# File 'lib/vivlio/starter/cli/post_process/html_parser.rb', line 39

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ドキュメントをファイルに保存



44
45
46
# File 'lib/vivlio/starter/cli/post_process/html_parser.rb', line 44

def save_html_document(path, doc)
  File.write(path, render_html_document(doc), encoding: 'utf-8')
end

.zero_width_text?(text) ⇒ Boolean

ゼロ幅スペース等のみで構成されているかを判定する

Returns:

  • (Boolean)


49
50
51
# File 'lib/vivlio/starter/cli/post_process/html_parser.rb', line 49

def zero_width_text?(text)
  text.gsub(/[\u200B\u200C\u200D\u2060\uFEFF\u180E]/, '').strip.empty?
end