Class: Rawfeed::Datelang::Main
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Rawfeed::Datelang::Main
- Defined in:
- lib/rawfeed/datelang.rb
Instance Method Summary collapse
-
#initialize(tag_name, text, tokens) ⇒ Main
constructor
A new instance of Main.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, text, tokens) ⇒ Main
Returns a new instance of Main.
9 10 11 12 |
# File 'lib/rawfeed/datelang.rb', line 9 def initialize(tag_name, text, tokens) super @markup = text.strip end |
Instance Method Details
#render(context) ⇒ Object
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 44 45 46 |
# File 'lib/rawfeed/datelang.rb', line 14 def render(context) site = context.registers[:site] page = context.registers[:page] || {} = site.data['options'] locale = .dig("datelang", "lang") || "en_US" json_path = File.join(site.source, "assets", "json", "datelang.json") json_path = File.("../../assets/json/datelang.json", __dir__) unless File.exist?(json_path) return "[datelang error: datelang.json not found]" unless File.exist?(json_path) translations = JSON.parse(File.read(json_path)) data = translations[locale] || translations["en_US"] args = parse_args(@markup, context) date_input = args[:date] || page["date"] || page[:date] format = args[:format] || "%b %-d, %Y" return "[datelang: no date]" unless date_input && date_input.to_s.strip != "" date = parse_date(date_input) return "[datelang: invalid date '#{date_input}']" unless date formatted = date.strftime(format) # Automatically detects month type based on format if format.include?("%B") replace_months(formatted, data, :full) else replace_months(formatted, data, :short) end end |