Class: Neruda::OrgFile
- Inherits:
-
Object
- Object
- Neruda::OrgFile
- Extended by:
- OrgFileClassMethods
- Includes:
- OrgFileExtracter, OrgFileHtmlizer
- Defined in:
- lib/neruda/org_file.rb
Overview
Handles org files.
This class is responsible for reading or writing existing or new org files, and formating their content to be used on the generated website.
Instance Attribute Summary collapse
-
#author ⇒ String
readonly
The author of the current org document, taken from the ~#+author:~ header.
-
#date ⇒ DateTime
readonly
The date and time of the current org document, taken from the ~#+date:~ header.
-
#excerpt ⇒ String
readonly
The description of this org document, taken from the ~#+description:~ header.
-
#file ⇒ String
readonly
The relative path to the source of this document.
-
#html_file ⇒ String
readonly
The relative path to the generated html file of this document.
-
#keywords ⇒ Array
readonly
The keywords list of the current org document, taken from the ~#+keywords:~ header.
-
#lang ⇒ String
readonly
The locale of the current org document, taken from the ~#+language:~ header.
-
#notime ⇒ Boolean
readonly
Wether a time has been extracted from the current org document ~#+date:~ header.
-
#project ⇒ String
readonly
The project owning this document.
-
#subtitle ⇒ String
readonly
The subtitle of the current org document, taken from the ~#+subtitle:~ header.
-
#title ⇒ String
readonly
The title of the current org document, taken from the ~#+title:~ header.
-
#url ⇒ String
readonly
The url of this document, build from the ~domain~ settings and the above @html_file attribute.
Instance Method Summary collapse
-
#datestring(dateformat = :full, year: true) ⇒ String
Returns the current OrgFile instance DateTime as a String.
-
#format(string) ⇒ String
Formats given ~string~ with values of the current OrgFile.
-
#initialize(file_name, opts = {}) ⇒ Neruda::OrgFile
constructor
Prepares the file named by ~file_name~ for read and write operations.
-
#timekey ⇒ String
Returns a String representation of the document date, which aims to be used to sort several OrgFiles.
-
#write ⇒ Integer
Writes the current OrgFile content to the underlying file.
Methods included from OrgFileClassMethods
project_for_source, slug, source_for_target, target_for_source
Methods included from OrgFileHtmlizer
Constructor Details
#initialize(file_name, opts = {}) ⇒ Neruda::OrgFile
Prepares the file named by ~file_name~ for read and write
operations.
If the file ~file_name~ does not exist, the new instance may be populated by data given in the ~opts~ parameter.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/neruda/org_file.rb', line 118 def initialize(file_name, opts = {}) file_name = nil if file_name == '' @file = file_name @html_file = nil @url = nil @project = opts.delete :project = opts build_html_file_and_url if @file && File.exist?(@file) extract_data else init_empty_file end end |
Instance Attribute Details
#author ⇒ String (readonly)
The author of the current org document, taken from the ~#+author:~
header.
If the current document doesn’t have any authorship information, the one from the ~config.yml~ file will be used instead
42 43 44 |
# File 'lib/neruda/org_file.rb', line 42 def end |
#date ⇒ DateTime (readonly)
Returns the date and time of the current org document, taken from the ~#+date:~ header.
29 30 31 |
# File 'lib/neruda/org_file.rb', line 29 def date @date end |
#excerpt ⇒ String (readonly)
Returns the description of this org document, taken from the ~#+description:~ header.
50 51 52 |
# File 'lib/neruda/org_file.rb', line 50 def excerpt @excerpt end |
#file ⇒ String (readonly)
Returns the relative path to the source of this document.
63 64 65 |
# File 'lib/neruda/org_file.rb', line 63 def file @file end |
#html_file ⇒ String (readonly)
Returns the relative path to the generated html file of this document.
67 68 69 |
# File 'lib/neruda/org_file.rb', line 67 def html_file @html_file end |
#keywords ⇒ Array (readonly)
Returns the keywords list of the current org document, taken from the ~#+keywords:~ header.
46 47 48 |
# File 'lib/neruda/org_file.rb', line 46 def keywords @keywords end |
#lang ⇒ String (readonly)
The locale of the current org document, taken from the
~#+language:~ header.
If the current document doesn’t have any language information, the one from the ~config.yml~ file will be used instead, or “en” by default.
60 61 62 |
# File 'lib/neruda/org_file.rb', line 60 def lang @lang end |
#notime ⇒ Boolean (readonly)
Returns wether a time has been extracted from the current org document ~#+date:~ header.
33 34 35 |
# File 'lib/neruda/org_file.rb', line 33 def notime @notime end |
#project ⇒ String (readonly)
Returns the project owning this document.
74 75 76 |
# File 'lib/neruda/org_file.rb', line 74 def project @project end |
#subtitle ⇒ String (readonly)
Returns the subtitle of the current org document, taken from the ~#+subtitle:~ header.
25 26 27 |
# File 'lib/neruda/org_file.rb', line 25 def subtitle @subtitle end |
#title ⇒ String (readonly)
Returns the title of the current org document, taken from the ~#+title:~ header.
21 22 23 |
# File 'lib/neruda/org_file.rb', line 21 def title @title end |
#url ⇒ String (readonly)
Returns the url of this document, build from the ~domain~ settings and the above @html_file attribute.
71 72 73 |
# File 'lib/neruda/org_file.rb', line 71 def url @url end |
Instance Method Details
#datestring(dateformat = :full, year: true) ⇒ String
Returns the current OrgFile instance DateTime as a String.
This method accepts three values for the ~dateformat~ parameter:
- ~:full~ (or ~:long~)
-
outputs a complete date and time
representation, localized through R18n;
- ~:short~
-
outputs a short date representation (without time),
localized with R18n;
- ~:rfc3339~
-
outputs the RFC 3339 date and time representation,
used in atom feed.
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/neruda/org_file.rb', line 181 def datestring(dateformat = :full, year: true) return '' if @date.nil? return R18n.l @date.to_date if dateformat == :short return @date.rfc3339 if dateformat == :rfc3339 locale = R18n.get.locale long_fmt = R18n.t.neruda.index.full_date_format( date: locale.format_date_full(@date, year) ) unless @notime long_fmt = R18n.t.neruda.index.full_date_with_time_format( date: long_fmt, time: locale.time_format.delete('_').strip ) end locale.strftime(@date, long_fmt) end |
#format(string) ⇒ String
Formats given ~string~ with values of the current OrgFile.
This method expects to find percent-tags in the given ~string~ and replace them by their corresponding value.
*** Format:
- %a
-
the raw author name
- %A
-
the HTML rendering of the author name, equivalent to ~<span class=“author”>%a</span>~
- %d
-
the ~:short~ date HTML representation, equivalent to ~<time datetime=“%I”>%i</time>~
- %D
-
the ~:full~ date and time HTML representation
- %i
-
the raw ~:short~ date and time
- %I
-
the raw ~:rfc3339~ date and time
- %k
-
the keywords separated by a comma
- %K
-
the HTML list rendering of the keywords
- %l
-
the lang of the document
- %L
-
the license information, taken from the Config#settings
- %n
-
the Neruda name and version
- %N
-
the Neruda name and version with a link to the project home on the name
- %s
-
the subtitle of the document
- %t
-
the title of the document
- %u
-
the web path to the related published HTML document
- %x
-
the raw description (eXcerpt)
- %X
-
the description, enclosed in an HTML ~p~ tag, equivalent to ~<p>%x</p>~
rubocop:disable Metrics/MethodLength rubocop:disable Layout/LineLength
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/neruda/org_file.rb', line 234 def format(string) string.gsub('%a', ) .gsub('%A', ) .gsub('%d', date_to_html(:short)) .gsub('%D', date_to_html) .gsub('%i', datestring(:short)) .gsub('%I', datestring(:rfc3339)) .gsub('%k', @keywords.join(', ')) .gsub('%K', keywords_to_html) .gsub('%l', @lang) .gsub('%L', (Neruda::Config.settings['license'] || '').gsub(/\s+/, ' ').strip) .gsub('%n', "Neruda #{Neruda::VERSION}") .gsub('%N', "<a href=\"https://git.umaneti.net/neruda/about/\">Neruda</a> #{Neruda::VERSION}") .gsub('%s', @subtitle) .gsub('%t', @title) .gsub('%u', @html_file || '') .gsub('%x', @excerpt) .gsub('%X', "<p>#{@excerpt}</p>") end |
#timekey ⇒ String
Returns a String representation of the document date, which aims
to be used to sort several OrgFiles.
The format used for the key is ~%Y%m%d%H%M%S~. If the current OrgFile instance does not have a date, this mehod return ~00000000000000~. If the current OrgFile instance does not have time information, the date is padded with zeros.
160 161 162 163 |
# File 'lib/neruda/org_file.rb', line 160 def timekey return '00000000000000' if @date.nil? @date.strftime('%Y%m%d%H%M%S') end |
#write ⇒ Integer
Writes the current OrgFile content to the underlying file.
The intermediate parent folders are created if necessary.
262 263 264 265 266 267 |
# File 'lib/neruda/org_file.rb', line 262 def write raise TypeError, 'no conversion from nil file name to path.' if @file.nil? file_dir = File.dirname @file FileUtils.mkdir_p file_dir unless Dir.exist? file_dir IO.write @file, @content end |