Class: JekyllImport::Importers::Jrnl
- Inherits:
-
JekyllImport::Importer
- Object
- JekyllImport::Importer
- JekyllImport::Importers::Jrnl
- Defined in:
- lib/jekyll-import/importers/jrnl.rb
Class Method Summary collapse
-
.create_filename(date, slug, extension) ⇒ Object
generate filename.
-
.create_meta(layout, title, date) ⇒ Object
Prepare YAML meta data.
-
.create_slug(title) ⇒ Object
generate slug.
-
.get_date(content, offset) ⇒ Object
strip timestamp from the dateline.
-
.get_post_content(content) ⇒ Object
strip body from jrnl entry.
-
.get_title(content, offset) ⇒ Object
strip title from the dateline.
-
.process(options) ⇒ Object
Reads a jrnl file and creates a new post for each entry The following overrides are available: :file path to input file :time_format the format used by the jrnl configuration :extension the extension format of the output files :layout explicitly set the layout of the output.
- .require_deps ⇒ Object
- .specify_options(c) ⇒ Object
-
.write_file(filename, meta, body) ⇒ Object
Writes given data to file.
Methods inherited from JekyllImport::Importer
inherited, run, stringify_keys, subclasses
Class Method Details
.create_filename(date, slug, extension) ⇒ Object
generate filename
80 81 82 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 80 def self.create_filename(date, slug, extension) return "#{Time.parse(date).strftime("%Y-%m-%d")}-#{slug}.#{extension}" end |
.create_meta(layout, title, date) ⇒ Object
Prepare YAML meta data
layout - name of the layout title - title of the entry date - date of entry creation
Examples
("post", "Entry 1", "2013-01-01 13:00")
# => "---\nlayout: post\ntitle: Entry 1\ndate: 2013-01-01 13:00\n"
Returns array converted to YAML
96 97 98 99 100 101 102 103 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 96 def self.(layout, title, date) data = { 'layout' => layout, 'title' => title, 'date' => Time.parse(date).strftime("%Y-%m-%d %H:%M %z") }.to_yaml return data; end |
.create_slug(title) ⇒ Object
generate slug
75 76 77 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 75 def self.create_slug(title) return title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') end |
.get_date(content, offset) ⇒ Object
strip timestamp from the dateline
65 66 67 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 65 def self.get_date(content, offset) return content[0, offset] end |
.get_post_content(content) ⇒ Object
strip body from jrnl entry
60 61 62 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 60 def self.get_post_content(content) return content[1] end |
.get_title(content, offset) ⇒ Object
strip title from the dateline
70 71 72 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 70 def self.get_title(content, offset) return content[offset + 1, content.length] end |
.process(options) ⇒ Object
Reads a jrnl file and creates a new post for each entry The following overrides are available: :file path to input file :time_format the format used by the jrnl configuration :extension the extension format of the output files :layout explicitly set the layout of the output
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 26 def self.process() file = .fetch('file', "~/journal.txt") time_format = .fetch('time_format', "%Y-%m-%d %H:%M") extension = .fetch('extension', "md") layout = .fetch('layout', "post") date_length = Time.now.strftime(time_format).length # convert relative to absolute if needed file = File.(file) abort "The jrnl file was not found. Please make sure '#{file}' exists. You can specify a different file using the --file switch." unless File.file?(file) input = File.read(file) entries = input.split("\n\n"); entries.each do |entry| # split dateline and body # content[0] has the date and title # content[1] has the post body content = entry.split("\n") body = get_post_content(content) date = get_date(content[0], date_length) title = get_title(content[0], date_length) slug = create_slug(title) filename = create_filename(date, slug, extension) = (layout, title, date) # prepare YAML meta data write_file(filename, , body) # write to file end end |
.require_deps ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 5 def self.require_deps JekyllImport.require_with_fallback(%w[ time rubygems safe_yaml ]) end |
.specify_options(c) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 13 def self.(c) c.option 'file', '--file FILENAME', 'Journal file (default: "~/journal.txt")' c.option 'time_format', '--time_format FORMAT', 'Time format of your journal (default: "%Y-%m-%d %H:%M")' c.option 'extension', '--extension EXT', 'Output extension (default: "md")' c.option 'layout', '--layout NAME', 'Output post layout (default: "post")' end |
.write_file(filename, meta, body) ⇒ Object
Writes given data to file
filename - name of the output file meta - YAML header data body - jrnl entry content
Examples
write_file("2013-01-01-entry-1.md", "---\nlayout: post\ntitle: Entry 1\ndate: 2013-01-01 13:00\n", "This is the first entry for my new journal")
Writes file to _posts/filename
116 117 118 119 120 121 122 |
# File 'lib/jekyll-import/importers/jrnl.rb', line 116 def self.write_file(filename, , body) File.open("_posts/#{filename}", "w") do |f| f.puts f.puts "---\n\n" f.puts body end end |