# encoding: utf-8 module Octopress module Date # Русская локализация: MONTHNAMES_RU = [nil, "Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августа", "Сентября", "Октября", "Ноября", "Декабря" ] ABBR_MONTHNAMES_RU = [nil, "Янв", "Фев", "Мар", "Апр", "Май", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек" ] DAYNAMES_RU = [ "Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота" ] ABBR_DAYNAMES_RU = [ "Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб" ] # Returns a datetime if the input is a string def datetime(date) if date.class == String date = Time.parse(date) end date end # В _config.yml должно быть задано: date_format: ordinal def ordinalize(date) # Задаем наш формат выдачи даты format_date(date, "%e %B %Y, %H:%M, %a") # 20 Авг 2019 end # Formats date either as ordinal or by given date format # Adds %o as ordinal representation of the day def format_date(date, format) date = datetime(date) if format.nil? || format.empty? || format == "ordinal" date_formatted = ordinalize(date) else format.gsub!(/%a/, ABBR_DAYNAMES_RU[date.wday]) format.gsub!(/%A/, DAYNAMES_RU[date.wday]) format.gsub!(/%b/, ABBR_MONTHNAMES_RU[date.mon]) format.gsub!(/%B/, MONTHNAMES_RU[date.mon]) date_formatted = date.strftime(format) end date_formatted end end end module Jekyll class Post include Octopress::Date def to_liquid(attrs = nil) date_format = self.site.config['date_format'] new_datas = { "title" => self.data['title'] || self.slug.split('-').select {|w| w.capitalize! || w }.join(' '), "url" => self.url, "date" => self.date, # Monkey patch "date_formatted" => format_date(self.date, date_format), "updated_formatted" => self.data.has_key?('updated') ? format_date(self.data['updated'], date_format) : nil, "id" => self.id, "categories" => self.categories, "next" => self.next, "previous" => self.previous, "tags" => self.tags, "content" => self.content } Utils.deep_merge_hashes(self.data, new_datas) end end class Page include Octopress::Date # Initialize a new Page. # # site - The Site object. # base - The String path to the source. # dir - The String path between the source and the file. # name - The String filename of the file. def initialize(site, base, dir, name) @site = site @base = base @dir = dir @name = name self.process(name) self.read_yaml(File.join(base, dir), name) # Monkey patch date_format = self.site.config['date_format'] self.data['date_formatted'] = format_date(self.data['date'], date_format) if self.data.has_key?('date') self.data['updated_formatted'] = format_date(self.data['updated'], date_format) if self.data.has_key?('updated') end end module Filters include Octopress::Date def date_ru(date, format) format_date(date, format) end end end