class PnoteClient::Documents::Hml

Attributes

bin_items[R]
paragraphs[R]
styles[R]

Public Class Methods

new(styles: [], paragraphs: [], bin_items: []) click to toggle source
# File lib/pnote_client/documents/hml.rb, line 48
def initialize(styles: [], paragraphs: [], bin_items: [])
  @styles = styles
  @paragraphs = paragraphs
  @bin_items = bin_items
end
parse(hml_content) click to toggle source
# File lib/pnote_client/documents/hml.rb, line 13
def self.parse(hml_content)
  styles = []
  paragraphs = []
  bin_items = []
  doc = Nokogiri::XML(hml_content)
  unless doc.errors.empty?
    raise InvalidXMLError.new(doc.errors.first.message)
  end

  # 빈 TEXT 삭제
  doc.xpath("//P[TEXT[not(node())]]").each do |p|
    p.remove
  end

  style_tags = doc.xpath("//STYLELIST/STYLE")
  style_tags.each do |style_tag|
    styles << Style.from_tag(style_tag)
  end

  pg_tags = doc.xpath("//SECTION/P")
  pg_tags.each do |pg_tag|
    paragraphs << Paragraph.from_tag(pg_tag)
  end

  bin_item_tags = doc.xpath("//BINITEM")
  bin_item_tags.each_with_index do |bin_item_tag, index|
    bin_item_id = index + 1
    if bin_item_tag['Type'] == 'Embedding'
      bin_items << EmbeddingBinaryItem.new(bin_item_id, bin_item_tag, doc)
    end
  end

  return self.new(styles: styles, paragraphs: paragraphs, bin_items: bin_items)
end