Module: VivlioStarter::CLI::PostProcessCommands::TerminalBlockConverter

Defined in:
lib/vivlio_starter/cli/post_process/terminal_block_converter.rb

Overview

</code>

Constant Summary collapse

TERMINAL_PRE_SELECTOR =

前処理が付けた独自言語名。MarkdownTransformer::TERMINAL_FENCE_LANG と対。

'pre.language-vs-terminal'

Class Method Summary collapse

Class Method Details

.convert_terminal_blocks!(html_file) ⇒ Object

Parameters:

  • html_file (String)

    HTML ファイルのパス



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vivlio_starter/cli/post_process/terminal_block_converter.rb', line 37

def convert_terminal_blocks!(html_file)
  content = File.read(html_file, encoding: 'utf-8')
  doc = HtmlParser.parse_html_document(content)

  targets = doc.css(TERMINAL_PRE_SELECTOR)
  return if targets.empty?

  targets.each { wrap_pre_in_terminal_div(it, doc) }

  HtmlParser.save_html_document(html_file, doc)
  Common.log_success("#{html_file}: terminal ブロックを #{targets.size} 件変換しました")
end

.wrap_pre_in_terminal_div(pre, doc) ⇒ Object

<code …>…



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vivlio_starter/cli/post_process/terminal_block_converter.rb', line 52

def wrap_pre_in_terminal_div(pre, doc)
  # <code> ラッパは畳む(テキストノードのまま子を引き上げる)
  code = pre.at_css('code')
  code.replace(code.children) if code

  pre.remove_attribute('class')

  wrapper = Nokogiri::XML::Node.new('div', doc)
  wrapper['class'] = 'terminal'
  pre.add_previous_sibling(wrapper)
  wrapper.add_child(pre)
end