Class: Wp2txt::MagicWordExpander

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

Overview

Expands MediaWiki magic words to their actual values Supports: page context variables, date/time variables, string functions

Constant Summary collapse

PAGE_CONTEXT_WORDS =

Page context magic words (case-insensitive)

{
  "PAGENAME" => :pagename,
  "PAGENAMEE" => :pagename_encoded,
  "FULLPAGENAME" => :fullpagename,
  "FULLPAGENAMEE" => :fullpagename_encoded,
  "BASEPAGENAME" => :basepagename,
  "BASEPAGENAMEE" => :basepagename_encoded,
  "ROOTPAGENAME" => :rootpagename,
  "ROOTPAGENAMEE" => :rootpagename_encoded,
  "SUBPAGENAME" => :subpagename,
  "SUBPAGENAMEE" => :subpagename_encoded,
  "TALKPAGENAME" => :talkpagename,
  "TALKPAGENAMEE" => :talkpagename_encoded,
  "SUBJECTPAGENAME" => :subjectpagename,
  "SUBJECTPAGENAMEE" => :subjectpagename_encoded,
  "ARTICLEPAGENAME" => :subjectpagename,
  "ARTICLEPAGENAMEE" => :subjectpagename_encoded,
  "NAMESPACE" => :namespace,
  "NAMESPACEE" => :namespace_encoded,
  "NAMESPACENUMBER" => :namespace_number,
  "TALKSPACE" => :talkspace,
  "TALKSPACEE" => :talkspace_encoded,
  "SUBJECTSPACE" => :subjectspace,
  "SUBJECTSPACEE" => :subjectspace_encoded,
  "ARTICLESPACE" => :subjectspace,
  "ARTICLESPACEE" => :subjectspace_encoded
}.freeze
DATETIME_WORDS =

Date/time magic words

{
  "CURRENTYEAR" => :current_year,
  "CURRENTMONTH" => :current_month,
  "CURRENTMONTH1" => :current_month1,
  "CURRENTMONTHNAME" => :current_month_name,
  "CURRENTMONTHNAMEGEN" => :current_month_name,
  "CURRENTMONTHABBREV" => :current_month_abbrev,
  "CURRENTDAY" => :current_day,
  "CURRENTDAY2" => :current_day2,
  "CURRENTDOW" => :current_dow,
  "CURRENTDAYNAME" => :current_day_name,
  "CURRENTTIME" => :current_time,
  "CURRENTHOUR" => :current_hour,
  "CURRENTWEEK" => :current_week,
  "CURRENTTIMESTAMP" => :current_timestamp,
  # Local variants (same as current for our purposes)
  "LOCALYEAR" => :current_year,
  "LOCALMONTH" => :current_month,
  "LOCALMONTH1" => :current_month1,
  "LOCALMONTHNAME" => :current_month_name,
  "LOCALMONTHNAMEGEN" => :current_month_name,
  "LOCALMONTHABBREV" => :current_month_abbrev,
  "LOCALDAY" => :current_day,
  "LOCALDAY2" => :current_day2,
  "LOCALDOW" => :current_dow,
  "LOCALDAYNAME" => :current_day_name,
  "LOCALTIME" => :current_time,
  "LOCALHOUR" => :current_hour,
  "LOCALWEEK" => :current_week,
  "LOCALTIMESTAMP" => :current_timestamp
}.freeze
STRING_FUNCTIONS =

String function magic words (with arguments)

%w[
  lc uc lcfirst ucfirst
  padleft padright
  anchorencode urlencode
  plural grammar gender
  int formatnum
].freeze
MONTH_NAMES =

Month names for expansion

%w[
  January February March April May June
  July August September October November December
].freeze
MONTH_ABBREVS =
%w[
  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
].freeze
DAY_NAMES =
%w[
  Sunday Monday Tuesday Wednesday Thursday Friday Saturday
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(title, namespace: "", dump_date: nil) ⇒ MagicWordExpander

Returns a new instance of MagicWordExpander.



94
95
96
97
98
# File 'lib/wp2txt/magic_words.rb', line 94

def initialize(title, namespace: "", dump_date: nil)
  @title = title || ""
  @namespace = namespace || ""
  @dump_date = dump_date || Time.now
end

Instance Method Details

#expand(text) ⇒ Object

Main expansion method - expands all supported magic words in text



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wp2txt/magic_words.rb', line 101

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

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

  result = text.dup

  # Expand simple magic words: {{PAGENAME}}, {{CURRENTYEAR}}, etc.
  result = expand_simple_magic_words(result)

  # Expand string functions: {{lc:Text}}, {{uc:Text}}, etc.
  result = expand_string_functions(result)

  # Expand #titleparts parser function
  result = expand_titleparts(result)

  result
end