HansPetter.no :blogg

Norsk dato i Jekyll

Kanskje ikke den mest elegante måten, men det virker.

Legg følgende i en fil i mappen _plugins/:

module Jekyll
  module NorskDato
    # puts Jekyll.configuration.inspect
    # ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    TRANSLATED_ABBR_DAYNAMES = %w{ Søn Man Tir Ons Tor Fre Lør}

    # ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    TRANSLATED_ABBR_MONTHNAMES = %w{ Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Des }
    # ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    TRANSLATED_DAYNAMES = %w{ Søndag Mandag Tirsdag Onsdag Torsdag Fredag Lørdag }
    # ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    TRANSLATED_MONTHNAMES = %w{ Januar Februar Mars April May Juni Juli August September Oktober November Desember }
    
    def til_norsk input
      lexicon = Date::ABBR_DAYNAMES.zip(TRANSLATED_ABBR_DAYNAMES) + 
      					Date::ABBR_MONTHNAMES.compact.zip(TRANSLATED_ABBR_MONTHNAMES) +
      					Date::DAYNAMES.zip(TRANSLATED_DAYNAMES) +
      					Date::MONTHNAMES.compact.zip(TRANSLATED_MONTHNAMES)

      lexicon = lexicon.to_h.reject! { |k,v| k == v }

      terms = lexicon.keys.compact.map{ |k| Regexp.escape(k) }
      pattern = Regexp.new terms.join("|")
      
      input.gsub(pattern, lexicon)
    end
  end
end

Liquid::Template.register_filter(Jekyll::NorskDato)

Filteret brukes slik:

{% raw %}
<span class="post-meta">
    {{ post.date | date: "%-d. %B %Y" | til_norsk }}
</span>
{% endraw %}