Class: Wp2txt::TemplateExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/wp2txt/template_expander.rb

Overview

Expands common MediaWiki templates to their text representation Handles date templates, convert templates, and other common patterns

Constant Summary collapse

MONTH_NAMES =
%w[
  January February March April May June
  July August September October November December
].freeze
CONVERSIONS =

Unit conversion factors

{
  # Length
  ["km", "mi"] => 0.621371,
  ["mi", "km"] => 1.60934,
  ["m", "ft"] => 3.28084,
  ["ft", "m"] => 0.3048,
  ["cm", "in"] => 0.393701,
  ["in", "cm"] => 2.54,
  ["mm", "in"] => 0.0393701,
  ["in", "mm"] => 25.4,
  ["yd", "m"] => 0.9144,
  ["m", "yd"] => 1.09361,
  # Weight
  ["kg", "lb"] => 2.20462,
  ["lb", "kg"] => 0.453592,
  ["g", "oz"] => 0.035274,
  ["oz", "g"] => 28.3495,
  ["t", "lb"] => 2204.62,
  ["lb", "t"] => 0.000453592,
  # Temperature (special handling)
  ["C", "F"] => :celsius_to_fahrenheit,
  ["°C", "°F"] => :celsius_to_fahrenheit,
  ["F", "C"] => :fahrenheit_to_celsius,
  ["°F", "°C"] => :fahrenheit_to_celsius,
  # Area
  ["km2", "sqmi"] => 0.386102,
  ["sqmi", "km2"] => 2.58999,
  ["ha", "acre"] => 2.47105,
  ["acre", "ha"] => 0.404686,
  ["m2", "sqft"] => 10.7639,
  ["sqft", "m2"] => 0.092903,
  # Speed
  ["km/h", "mph"] => 0.621371,
  ["mph", "km/h"] => 1.60934,
  ["m/s", "km/h"] => 3.6,
  ["km/h", "m/s"] => 0.277778,
  # Volume
  ["l", "gal"] => 0.264172,
  ["gal", "l"] => 3.78541,
  ["ml", "floz"] => 0.033814,
  ["floz", "ml"] => 29.5735
}.freeze
UNIT_DISPLAY =

Unit display names

{
  "km" => "km",
  "mi" => "mi",
  "m" => "m",
  "ft" => "ft",
  "cm" => "cm",
  "in" => "in",
  "mm" => "mm",
  "yd" => "yd",
  "kg" => "kg",
  "lb" => "lb",
  "g" => "g",
  "oz" => "oz",
  "t" => "t",
  "C" => "°C",
  "°C" => "°C",
  "F" => "°F",
  "°F" => "°F",
  "km2" => "km²",
  "sqmi" => "sq mi",
  "ha" => "ha",
  "acre" => "acres",
  "m2" => "",
  "sqft" => "sq ft",
  "km/h" => "km/h",
  "mph" => "mph",
  "m/s" => "m/s",
  "l" => "L",
  "gal" => "gal",
  "ml" => "mL",
  "floz" => "fl oz"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(reference_date: nil, preserve_unknown: false) ⇒ TemplateExpander

Returns a new instance of TemplateExpander.



90
91
92
93
# File 'lib/wp2txt/template_expander.rb', line 90

def initialize(reference_date: nil, preserve_unknown: false)
  @reference_date = reference_date || Time.now
  @preserve_unknown = preserve_unknown
end

Instance Method Details

#expand(text) ⇒ Object

Main expansion method



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/wp2txt/template_expander.rb', line 96

def expand(text)
  return text if text.nil? || text.empty?

  # Early exit: no templates to expand
  return text unless text.include?("{{")

  result = text.dup

  # Process templates from innermost to outermost
  max_iterations = 10
  iteration = 0

  while result.include?("{{") && iteration < max_iterations
    previous = result.dup
    result = expand_templates_single_pass(result)
    break if result == previous
    iteration += 1
  end

  result
end