Implement nested lists
All checks were successful
CI Pipeline / build (pull_request) Successful in 13s
All checks were successful
CI Pipeline / build (pull_request) Successful in 13s
This commit is contained in:
@@ -93,12 +93,10 @@ module Notare
|
||||
end
|
||||
|
||||
def li(text = nil, &block)
|
||||
item = Nodes::ListItem.new([], list_type: @current_list.type, num_id: @current_list.num_id)
|
||||
if block
|
||||
with_target(item, &block)
|
||||
elsif text
|
||||
item.add_run(Nodes::Run.new(text, **current_formatting))
|
||||
end
|
||||
current_type = @list_type_stack.last
|
||||
item = Nodes::ListItem.new([], list_type: current_type, num_id: @current_list.num_id, level: @list_level)
|
||||
item.add_run(Nodes::Run.new(text, **current_formatting)) if text
|
||||
with_target(item, &block) if block
|
||||
@current_list.add_item(item)
|
||||
end
|
||||
|
||||
@@ -134,15 +132,31 @@ module Notare
|
||||
|
||||
def list(type, &block)
|
||||
@num_id_counter ||= 0
|
||||
@num_id_counter += 1
|
||||
mark_has_lists!
|
||||
@list_level ||= 0
|
||||
@list_type_stack ||= []
|
||||
|
||||
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
|
||||
previous_list = @current_list
|
||||
@current_list = list_node
|
||||
block.call
|
||||
@current_list = previous_list
|
||||
@nodes << list_node
|
||||
nested = !previous_list.nil?
|
||||
|
||||
if nested
|
||||
# Nested list: reuse parent list, push new type, increment level
|
||||
@list_level += 1
|
||||
@list_type_stack.push(type)
|
||||
block.call
|
||||
@list_type_stack.pop
|
||||
@list_level -= 1
|
||||
else
|
||||
# Top-level list: new List node
|
||||
@num_id_counter += 1
|
||||
mark_has_lists!
|
||||
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
|
||||
@list_type_stack.push(type)
|
||||
@current_list = list_node
|
||||
block.call
|
||||
@current_list = previous_list
|
||||
@list_type_stack.pop
|
||||
@nodes << list_node
|
||||
end
|
||||
end
|
||||
|
||||
def with_format(format, &block)
|
||||
|
||||
Reference in New Issue
Block a user