# frozen_string_literal: true
module Stepmod
module Utils
module Converters
class ModuleRef < Stepmod::Utils::Converters::Base
def convert(node, _state = {})
link_end = node["linkend"].to_s.split(":")
ref_id = link_end.last
text = node.text.gsub(/\s/, " ").squeeze(" ").strip
schema = link_end.first
if _state[:schema_and_entity].nil?
# puts "[module_ref] setting node state #{link_end.inspect}"
_state[:schema_and_entity] = schema
end
case link_end[1]
when "1_scope", "introduction"
# When we see this:
# Functional usage view
# Part definition relationship
# We convert into:
# <>
# <>
"<>"
when "3_definition"
# #23:
# When we see this:
# individual products
# We convert to this:
# {{individual products}}
"{{#{text}}}"
when "4_types"
# When we see this:
# activity_method_item
# We convert to this:
# <>
"<>"
when "4_entities", "f_usage_guide"
case link_end[2]
when "figure"
# When we see this:
# Figure 1
# We convert to this:
# <>
# When we see this (without a number):
# This Figure
# We convert to this:
# <>
create_ref(Figure.pattern(_state, ref_id), text)
when "table"
# When we see this:
# Table 1
# We convert to this:
# <>
# When we see this (without a number):
# This Table
# We convert to this:
# <>
create_ref(Table.pattern(_state, ref_id), text)
end
else
puts "[module_ref]: encountered unknown tag, #{link_end.join(':')}"
raise StandardError.new("[module_ref]: encountered unknown tag, #{link_end.join(':')}")
end
# puts "[module_ref] #{result}"
end
private
def create_ref(link, text)
if number_with_optional_prefix?(text)
"<<#{link}>>"
else
"<<#{link},#{text}>>"
end
end
def number_with_optional_prefix?(text)
/.*?\s*\(?\d+\)?/.match(text)
end
end
ReverseAdoc::Converters.register :module_ref, ModuleRef.new
end
end
end