Class: Coradoc::Html::Converters::ListItem

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/html/converters/list_item.rb

Overview

Converter for ListItem

Class Method Summary collapse

Methods inherited from Base

build_class_attribute, build_element, build_html_attributes, convert_content_to_html, convert_element_to_core, convert_node_to_core, escape_attribute, escape_html, extract_model_attributes, extract_node_attributes, extract_text_fallback, find_converter_class_by_name, find_converter_for_model, handle_unknown_content, render_core_abbreviation, render_core_annotation_block, render_core_bibliography, render_core_bibliography_entry, render_core_block, render_core_block_image, render_core_definition_item, render_core_definition_list, render_core_footnote, render_core_footnote_reference, render_core_inline_element, render_core_inline_image, render_core_list_block, render_core_list_item, render_core_span, render_core_structural_element, render_core_table_cell, render_core_table_row, render_core_term, render_core_toc, render_core_toc_entry, resolve_block_semantic_type, resolve_format_specific_semantic, transform_to_coremodel, treat_children

Class Method Details

.to_coradoc(node, state = {}) ⇒ Coradoc::CoreModel::ListItem

Convert HTML <li> to CoreModel::ListItem

Parameters:

  • node (Nokogiri::XML::Node)

    HTML node

  • state (Hash) (defaults to: {})

    Conversion state

Returns:

  • (Coradoc::CoreModel::ListItem)

    ListItem model



13
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
# File 'lib/coradoc/html/converters/list_item.rb', line 13

def to_coradoc(node, state = {})
  attrs = extract_node_attributes(node)

  item = Coradoc::CoreModel::ListItem.new
  item.id = attrs[:id] if attrs[:id]

  # Extract content and nested lists
  content_nodes = []
  nested_list = nil

  node.children.each do |child|
    case child.name
    when 'ul', 'ol'
      # This is a nested list
      nested_list = convert_node_to_core(child, state)
    else
      content_nodes << child unless child.text.strip.empty? && child.name == 'text'
    end
  end

  # Convert content nodes - collect as mixed content array
  if content_nodes.any?
    content = content_nodes.flat_map { |n| convert_node_to_core(n, state) }.compact
    # Store as children for mixed content
    item.children = content if content.any?
  end

  item.nested = nested_list if nested_list

  item
end

.to_html(model, state = {}) ⇒ String

Convert CoreModel::ListItem to HTML <li>

Parameters:

  • model (Coradoc::CoreModel::ListItem)

    ListItem model

  • state (Hash) (defaults to: {})

    Conversion state

Returns:

  • (String)

    HTML string



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/coradoc/html/converters/list_item.rb', line 49

def to_html(model, state = {})
  parts = []

  # Convert main content - check children first (mixed content), then content
  content_to_render = model.children&.any? ? model.children : model.content
  parts << convert_content_to_html(content_to_render, state) if content_to_render

  # Convert attached content
  if model.attached && !model.attached.empty?
    model.attached.each do |attached_item|
      parts << convert_content_to_html(attached_item, state)
    end
  end

  # Convert nested list
  parts << convert_content_to_html(model.nested, state) if model.nested

  attrs = {}
  attrs[:id] = model.id if model.id

  build_element('li', parts.join("\n"), attrs)
end