neruda 0.0.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +5 -5
  2. data/bin/pablo +135 -238
  3. data/lib/neruda/config.rb +137 -0
  4. data/lib/neruda/config/lisp_config.rb +254 -0
  5. data/lib/neruda/config/org-config.el +18 -0
  6. data/lib/neruda/config/ox-neruda.el +114 -0
  7. data/lib/neruda/emacs.rb +44 -0
  8. data/lib/neruda/index.rb +122 -0
  9. data/lib/neruda/index/atom_generator.rb +86 -0
  10. data/lib/neruda/index/org_generator.rb +115 -0
  11. data/lib/neruda/org_file.rb +299 -0
  12. data/lib/neruda/org_file/class_methods.rb +72 -0
  13. data/lib/neruda/org_file/extracter.rb +72 -0
  14. data/lib/neruda/org_file/htmlizer.rb +53 -0
  15. data/lib/neruda/preview.rb +55 -0
  16. data/lib/neruda/templater.rb +112 -0
  17. data/lib/neruda/utils.rb +212 -0
  18. data/lib/neruda/version.rb +6 -0
  19. data/lib/tasks/org.rake +84 -0
  20. data/lib/tasks/site.rake +86 -0
  21. data/lib/tasks/sync.rake +34 -0
  22. data/lib/tasks/tags.rake +19 -0
  23. data/locales/en.yml +37 -0
  24. data/locales/fr.yml +37 -0
  25. data/themes/default/css/htmlize.css +346 -0
  26. data/themes/default/css/style.css +153 -0
  27. data/themes/default/img/bottom.png +0 -0
  28. data/themes/default/img/tic.png +0 -0
  29. data/themes/default/img/top.png +0 -0
  30. metadata +153 -43
  31. data/README.md +0 -98
  32. data/docs/Rakefile.example +0 -4
  33. data/docs/config.yml.example +0 -17
  34. data/lib/assets/chapter.slim +0 -14
  35. data/lib/assets/index.slim +0 -13
  36. data/lib/assets/layout.slim +0 -17
  37. data/lib/assets/style.css +0 -199
  38. data/lib/neruda.rb +0 -106
  39. data/lib/neruda/chapter.rb +0 -26
  40. data/lib/neruda/url.rb +0 -14
  41. data/lib/tasks/book.rake +0 -60
  42. data/lib/tasks/capistrano/chapters.rake +0 -60
  43. data/lib/tasks/capistrano/sinatra.rake +0 -18
  44. data/lib/tasks/chapters.rake +0 -132
  45. data/lib/tasks/sinatra.rake +0 -36
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi'
4
+ require 'neruda/config'
5
+
6
+ module Neruda
7
+ # Embeds Atom feeds sepecific methods
8
+ module IndexAtomGenerator
9
+ def to_atom(index_name = 'index')
10
+ content = [atom_header(index_name)]
11
+ @index[index_name][0...10].each do |article|
12
+ content << atom_entry(article)
13
+ end
14
+ format '%<content>s</feed>', content: content.join("\n")
15
+ end
16
+
17
+ def write_atom(index_name)
18
+ return unless save?
19
+ slug = Neruda::OrgFile.slug index_name
20
+ FileUtils.mkdir_p "#{@pubdir}/feeds"
21
+ atomdest = "#{@pubdir}/feeds/#{slug}.xml"
22
+ IO.write(atomdest, to_atom(index_name))
23
+ end
24
+
25
+ private
26
+
27
+ # Render the Atom feed header.
28
+ #
29
+ # @param title [String] the title of the current atom feed
30
+ # @return [String] the Atom header as a String
31
+ def atom_header(title)
32
+ domain = Neruda::Config.settings['domain']
33
+ upddate = @date.rfc3339
34
+ if title == 'index'
35
+ slug = 'index'
36
+ tagurl = domain
37
+ title = Neruda::Config.settings['title'] || \
38
+ R18n.t.neruda.index.all_tags
39
+ else
40
+ slug = Neruda::OrgFile.slug(title)
41
+ tagurl = "#{domain}/tags/#{slug}.html"
42
+ title = @tags_names[title]
43
+ end
44
+ title = CGI.escapeHTML(title)
45
+ <<~ENDATOM
46
+ <?xml version="1.0" encoding="utf-8"?>
47
+ <feed xmlns="http://www.w3.org/2005/Atom"
48
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
49
+ xmlns:wfw="http://wellformedweb.org/CommentAPI/"
50
+ xml:lang="#{Neruda::Config.settings['lang']}">
51
+
52
+ <title>#{title}</title>
53
+ <link href="#{domain}/feeds/#{slug}.xml" rel="self" type="application/atom+xml"/>
54
+ <link href="#{tagurl}" rel="alternate" type="text/html" title="#{title}"/>
55
+ <updated>#{upddate}</updated>
56
+ <author><name>#{Neruda::Config.settings['author'] || ''}</name></author>
57
+ <id>urn:md5:#{Digest::MD5.hexdigest(domain)}</id>
58
+ <generator uri="https://git.umaneti.net/neruda/about/">Neruda</generator>
59
+ ENDATOM
60
+ end
61
+
62
+ # Render an Atom feed entry.
63
+ #
64
+ # @param article [Neruda::OrgFile] the related org document for this
65
+ # entry
66
+ # @return [String] the Atom entry as a String
67
+ def atom_entry(article)
68
+ keywords = article.keywords.map do |k|
69
+ "<dc:subject>#{CGI.escapeHTML(k)}</dc:subject>"
70
+ end.join
71
+ keywords += "\n " if keywords != ''
72
+ title = CGI.escapeHTML(article.title)
73
+ <<~ENDENTRY
74
+ <entry>
75
+ <title>#{title}</title>
76
+ <link href="#{article.url}" rel="alternate" type="text/html"
77
+ title="#{title}"/>
78
+ <id>urn:md5:#{Digest::MD5.hexdigest(article.timekey)}</id>
79
+ <published>#{article.datestring(:rfc3339)}</published>
80
+ <author><name>#{CGI.escapeHTML(article.author)}</name></author>
81
+ #{keywords}<content type="html">#{CGI.escapeHTML(article.excerpt)}</content>
82
+ </entry>
83
+ ENDENTRY
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neruda
4
+ # Embeds methods responsible for generating an org file for a given
5
+ # index.
6
+ module IndexOrgGenerator
7
+ def to_org(index_name = 'index', is_project: false)
8
+ return project_home_page(index_name) if is_project
9
+ return all_tags_index if index_name == 'index'
10
+ [org_header(index_name),
11
+ org_articles(@index[index_name])].join("\n")
12
+ end
13
+ alias_method :to_s, :to_org
14
+
15
+ def write_org(index_name)
16
+ return unless save?
17
+ slug = Neruda::OrgFile.slug index_name
18
+ FileUtils.mkdir 'tags' unless Dir.exist? 'tags'
19
+ content = to_org index_name
20
+ orgdest = "tags/#{slug}.org"
21
+ IO.write(orgdest, content)
22
+ end
23
+
24
+ private
25
+
26
+ def project_home_page(project_name)
27
+ content = [org_header(project_name, is_tag: false)]
28
+ if @projects[project_name]&.any?
29
+ content += org_articles(@projects[project_name])
30
+ end
31
+ content.join("\n")
32
+ end
33
+
34
+ def write_all_blog_home(verbose)
35
+ Neruda::Config.sources.each do |project|
36
+ next unless project['is_blog']
37
+ next unless Dir.exist?(project['path'])
38
+ warn "Generated blog home for #{project['name']}" if verbose
39
+ orgdest = format('%<root>s/index.org', root: project['path'])
40
+ IO.write(orgdest, to_org(project['name'], is_project: true))
41
+ end
42
+ end
43
+
44
+ def all_tags_index
45
+ content = [
46
+ org_header(R18n.t.neruda.index.all_tags, is_tag: false)
47
+ ]
48
+ sort_tags_by_name_and_weight.each do |t, tags|
49
+ content << ''
50
+ content << org_title(R18n.t.neruda.index.send(t), 'index-tags')
51
+ next if tags.empty?
52
+ tags.each do |k|
53
+ content << "- #{tag_published_url(k)} (#{@index[k].length})"
54
+ end
55
+ end
56
+ content.join("\n")
57
+ end
58
+
59
+ def tag_published_url(tag_name)
60
+ domain = Neruda::Config.settings['domain']
61
+ title = @tags_names[tag_name]
62
+ tag_link = "#{domain}/tags/#{tag_name}.html"
63
+ "[[#{tag_link}][#{title}]]"
64
+ end
65
+
66
+ def org_header(title = nil, is_tag: true)
67
+ if is_tag
68
+ title = @tags_names[title]
69
+ elsif title.nil? || title == 'index'
70
+ title = Neruda::Config.settings['title']
71
+ end
72
+ head = <<~HEADER
73
+ #+title: #{title}
74
+ #+author: #{Neruda::Config.settings['author']}
75
+ #+language: #{Neruda::Config.settings['lang']}
76
+ HEADER
77
+ head.strip
78
+ end
79
+
80
+ def org_articles(articles_list)
81
+ last_year = nil
82
+ articles_list.map do |article|
83
+ year_title = ''
84
+ year = article.timekey.slice(0, 4)
85
+ if year != last_year
86
+ year_title = format("\n%<title>s\n", title: org_title(year))
87
+ last_year = year
88
+ end
89
+ year_title + org_entry(article)
90
+ end
91
+ end
92
+
93
+ def org_entry(article)
94
+ line = "- *[[#{article.url}][#{article.title}]]*"
95
+ if article.date
96
+ art_date = article.datestring(:full, year: false)
97
+ published = R18n.t.neruda.index.published_on art_date
98
+ line += " / #{published}"
99
+ end
100
+ line += " \\\\\n #{article.excerpt}" if article.excerpt != ''
101
+ line
102
+ end
103
+
104
+ def org_title(year, html_class = 'index-year')
105
+ year = R18n.t.neruda.index.unsorted if year == '0000'
106
+ <<~ENDPROP
107
+ * #{year}
108
+ :PROPERTIES:
109
+ :HTML_CONTAINER_CLASS: #{html_class}
110
+ :UNNUMBERED: notoc
111
+ :END:
112
+ ENDPROP
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,299 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+ require 'fileutils'
5
+ # neruda/config is required by htmlizer
6
+ require 'neruda/org_file/htmlizer'
7
+ require 'neruda/org_file/extracter'
8
+ require 'neruda/org_file/class_methods'
9
+ require 'neruda/index'
10
+ require 'neruda/version'
11
+
12
+ module Neruda
13
+ # Handles org files.
14
+ #
15
+ # This class is responsible for reading or writing existing or new org
16
+ # files, and formating their content to be used on the generated
17
+ # website.
18
+ class OrgFile
19
+ # @return [String] the title of the current org document, taken from
20
+ # the ~#+title:~ header.
21
+ attr_reader :title
22
+
23
+ # @return [String] the subtitle of the current org document, taken
24
+ # from the ~#+subtitle:~ header.
25
+ attr_reader :subtitle
26
+
27
+ # @return [DateTime] the date and time of the current org document,
28
+ # taken from the ~#+date:~ header.
29
+ attr_reader :date
30
+
31
+ # @return [Boolean] wether a time has been extracted from the
32
+ # current org document ~#+date:~ header.
33
+ attr_reader :notime
34
+
35
+ # The author of the current org document, taken from the ~#+author:~
36
+ # header.
37
+ #
38
+ # If the current document doesn't have any authorship information,
39
+ # the one from the ~config.yml~ file will be used instead
40
+ #
41
+ # @return [String] the author name
42
+ attr_reader :author
43
+
44
+ # @return [Array] the keywords list of the current org document,
45
+ # taken from the ~#+keywords:~ header.
46
+ attr_reader :keywords
47
+
48
+ # @return [String] the description of this org document, taken from
49
+ # the ~#+description:~ header.
50
+ attr_reader :excerpt
51
+
52
+ # The locale of the current org document, taken from the
53
+ # ~#+language:~ header.
54
+ #
55
+ # If the current document doesn't have any language information, the
56
+ # one from the ~config.yml~ file will be used instead, or "en" by
57
+ # default.
58
+ #
59
+ # @return [String] the document lang
60
+ attr_reader :lang
61
+
62
+ # @return [String] the relative path to the source of this document.
63
+ attr_reader :file
64
+
65
+ # @return [String] the relative path to the generated html file of
66
+ # this document.
67
+ attr_reader :html_file
68
+
69
+ # @return [String] the url of this document, build from the ~domain~
70
+ # settings and the above {#html_file @html_file} attribute.
71
+ attr_reader :url
72
+
73
+ # @return [String] the project owning this document.
74
+ attr_reader :project
75
+
76
+ extend Neruda::OrgFileClassMethods
77
+
78
+ include Neruda::OrgFileExtracter
79
+ include Neruda::OrgFileHtmlizer
80
+
81
+ # Prepares the file named by ~file_name~ for read and write
82
+ # operations.
83
+ #
84
+ # If the file ~file_name~ does not exist, the new instance may be
85
+ # populated by data given in the ~opts~ parameter.
86
+ #
87
+ # @example
88
+ # File.exist? './test.org'
89
+ # => true
90
+ # o = Neruda::OrgFile.new('./test.org')
91
+ # => #<Neruda::OrgFile @file='./test.org'...>
92
+ # o.title
93
+ # => "This is an existing test file"
94
+ # File.exist? '/tmp/does_not_exist.org'
95
+ # => false
96
+ # o = Neruda::OrgFile.new('/tmp/does_not_exist.org')
97
+ # => #<Neruda::OrgFile @file='/tmp/does_not_exist.org'...>
98
+ # o.title
99
+ # => ""
100
+ # File.exist? '/tmp/other.org'
101
+ # => false
102
+ # o = Neruda::OrgFile.new('/tmp/other.org', title: 'New file')
103
+ # => #<Neruda::OrgFile @file='/tmp/other.org'...>
104
+ # o.title
105
+ # => "New file"
106
+ #
107
+ # @param file_name [String] path to the corresponding Org file
108
+ # @param opts [Hash] optional data to initialize new Org file
109
+ # @option opts [String] title ('') the title of the new Org file
110
+ # @option opts [String] author (system user or '') the author of the
111
+ # document
112
+ # @option opts [Boolean] verbose (false) if the
113
+ # {Neruda::OrgFileHtmlizer#publish publish} method should output
114
+ # emacs process messages
115
+ # @option opts [String] project the project owning this file
116
+ # must be stored
117
+ # @return [Neruda::OrgFile] the new instance of Neruda::OrgFile
118
+ def initialize(file_name, opts = {})
119
+ file_name = nil if file_name == ''
120
+ @file = file_name
121
+ @html_file = nil
122
+ @url = nil
123
+ @project = opts.delete :project
124
+ @options = opts
125
+ build_html_file_and_url
126
+ if @file && File.exist?(@file)
127
+ extract_data
128
+ else
129
+ init_empty_file
130
+ end
131
+ end
132
+
133
+ # Returns a String representation of the document date, which aims
134
+ # to be used to sort several OrgFiles.
135
+ #
136
+ # The format used for the key is ~%Y%m%d%H%M%S~. If the current
137
+ # OrgFile instance does not have a date, this mehod return
138
+ # ~00000000000000~. If the current OrgFile instance does not have
139
+ # time information, the date is padded with zeros.
140
+ #
141
+ # @example with the org header ~#+date: <2019-07-03 Wed 20:52:49>~
142
+ # org_file.date
143
+ # => #<DateTime: 2019-07-03T20:52:49+02:00...>
144
+ # org_file.timekey
145
+ # => "20190703205349"
146
+ #
147
+ # @example with the org header ~#+date: <2019-07-03 Wed>~
148
+ # org_file.date
149
+ # => #<DateTime: 2019-07-03T00:00:00+02:00...>
150
+ # org_file.timekey
151
+ # => "20190703000000"
152
+ #
153
+ # @example with no date header in the org file
154
+ # org_file.date
155
+ # => nil
156
+ # org_file.timekey
157
+ # => "00000000000000"
158
+ #
159
+ # @return [String] the document key
160
+ def timekey
161
+ return '00000000000000' if @date.nil?
162
+ @date.strftime('%Y%m%d%H%M%S')
163
+ end
164
+
165
+ # Returns the current OrgFile instance DateTime as a String.
166
+ #
167
+ # This method accepts three values for the ~dateformat~ parameter:
168
+ #
169
+ # - ~:full~ (or ~:long~) :: outputs a complete date and time
170
+ # representation, localized through R18n;
171
+ # - ~:short~ :: outputs a short date representation (without time),
172
+ # localized with R18n;
173
+ # - ~:rfc3339~ :: outputs the RFC 3339 date and time representation,
174
+ # used in atom feed.
175
+ #
176
+ # @param dateformat [Symbol] the format to use to convert DateTime
177
+ # into String
178
+ # @param year [Boolean] wether or not the ~:full~ format must
179
+ # contain the year
180
+ # @return [String] the document DateTime string representation
181
+ def datestring(dateformat = :full, year: true)
182
+ return '' if @date.nil?
183
+ return R18n.l @date.to_date if dateformat == :short
184
+ return @date.rfc3339 if dateformat == :rfc3339
185
+ locale = R18n.get.locale
186
+ long_fmt = R18n.t.neruda.index.full_date_format(
187
+ date: locale.format_date_full(@date, year)
188
+ )
189
+ unless @notime
190
+ long_fmt = R18n.t.neruda.index.full_date_with_time_format(
191
+ date: long_fmt, time: locale.time_format.delete('_').strip
192
+ )
193
+ end
194
+ locale.strftime(@date, long_fmt)
195
+ end
196
+
197
+ # Formats given ~string~ with values of the current OrgFile.
198
+ #
199
+ # This method expects to find percent-tags in the given ~string~ and
200
+ # replace them by their corresponding value.
201
+ #
202
+ # *** Format:
203
+ #
204
+ # - %a :: the raw author name
205
+ # - %A :: the HTML rendering of the author name, equivalent to
206
+ # ~<span class="author">%a</span>~
207
+ # - %d :: the ~:short~ date HTML representation, equivalent
208
+ # to ~<time datetime="%I">%i</time>~
209
+ # - %D :: the ~:full~ date and time HTML representation
210
+ # - %i :: the raw ~:short~ date and time
211
+ # - %I :: the raw ~:rfc3339~ date and time
212
+ # - %k :: the keywords separated by a comma
213
+ # - %K :: the HTML list rendering of the keywords
214
+ # - %l :: the lang of the document
215
+ # - %L :: the license information, taken from the
216
+ # {Neruda::Config#settings}
217
+ # - %n :: the Neruda name and version
218
+ # - %N :: the Neruda name and version with a link to the project
219
+ # home on the name
220
+ # - %s :: the subtitle of the document
221
+ # - %t :: the title of the document
222
+ # - %u :: the web path to the related published HTML document
223
+ # - %x :: the raw description (eXcerpt)
224
+ # - %X :: the description, enclosed in an HTML ~p~ tag, equivalent
225
+ # to ~<p>%x</p>~
226
+ #
227
+ # @example
228
+ # org_file.format("Article written by %a the %d")
229
+ # => "Article written by Alice Smith the Wednesday 3rd July"
230
+ #
231
+ # @return [String] the given ~string~ after replacement occurs
232
+ # rubocop:disable Metrics/MethodLength
233
+ # rubocop:disable Layout/LineLength
234
+ def format(string)
235
+ string.gsub('%a', @author)
236
+ .gsub('%A', author_to_html)
237
+ .gsub('%d', date_to_html(:short))
238
+ .gsub('%D', date_to_html)
239
+ .gsub('%i', datestring(:short))
240
+ .gsub('%I', datestring(:rfc3339))
241
+ .gsub('%k', @keywords.join(', '))
242
+ .gsub('%K', keywords_to_html)
243
+ .gsub('%l', @lang)
244
+ .gsub('%L', (Neruda::Config.settings['license'] || '').gsub(/\s+/, ' ').strip)
245
+ .gsub('%n', "Neruda #{Neruda::VERSION}")
246
+ .gsub('%N', "<a href=\"https://git.umaneti.net/neruda/about/\">Neruda</a> #{Neruda::VERSION}")
247
+ .gsub('%s', @subtitle)
248
+ .gsub('%t', @title)
249
+ .gsub('%u', @html_file || '')
250
+ .gsub('%x', @excerpt)
251
+ .gsub('%X', "<p>#{@excerpt}</p>")
252
+ end
253
+ # rubocop:enable Layout/LineLength
254
+ # rubocop:enable Metrics/MethodLength
255
+
256
+ # Writes the current OrgFile content to the underlying file.
257
+ #
258
+ # The intermediate parent folders are created if necessary.
259
+ #
260
+ # @return [Integer] the length written (as returned by the
261
+ # underlying ~IO.write~ method call)
262
+ def write
263
+ raise TypeError, 'no conversion from nil file name to path.' if @file.nil?
264
+ file_dir = File.dirname @file
265
+ FileUtils.mkdir_p file_dir unless Dir.exist? file_dir
266
+ IO.write @file, @content
267
+ end
268
+
269
+ private
270
+
271
+ def build_html_file_and_url
272
+ return if @file.nil?
273
+ @html_file = Neruda::OrgFile.target_for_source(
274
+ @file, @project, with_public_folder: false
275
+ )
276
+ @url = "#{Neruda::Config.settings['domain']}/#{@html_file}"
277
+ end
278
+
279
+ def init_empty_file
280
+ @title = @options[:title] || ''
281
+ @subtitle = ''
282
+ @date = DateTime.now
283
+ @notime = false
284
+ @author = @options[:author] || Neruda::Config.settings['author']
285
+ @keywords = []
286
+ @lang = @options[:lang] || Neruda::Config.settings['lang']
287
+ @excerpt = ''
288
+ body = @options[:content] || ''
289
+ @content = @options[:raw_content] || <<~ORG
290
+ #+title: #{@title}
291
+ #+date: <#{@date.strftime('%Y-%m-%d %a. %H:%M:%S')}>
292
+ #+author: #{@author}
293
+ #+language: #{@lang}
294
+
295
+ #{body}
296
+ ORG
297
+ end
298
+ end
299
+ end