Module: Journaltxt

Defined in:
lib/journaltxt.rb,
lib/journaltxt/parser.rb,
lib/journaltxt/version.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

DEFAULTS =
{ outpath: '.',
  date:    true,      # include date in (auto-)title
  verbose: false,
  name:    'Journal'
}
MAJOR =
1
MINOR =
0
PATCH =
1
VERSION =
[MAJOR,MINOR,PATCH].join('.')

Class Method Summary collapse

Class Method Details



14
15
16
# File 'lib/journaltxt/version.rb', line 14

def self.banner
  "journaltxt/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
end

.build(text, opts = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/journaltxt.rb', line 94

def self.build( text, opts={} )
  puts ":: Opts :::"
  pp opts

  outpath   = opts[:outpath]  || DEFAULTS[:outpath]
  name      = opts[:name]     || DEFAULTS[:name]
  add_date  = opts.fetch( :date, DEFAULTS[:date] )  ## special case boolean flag migth be false


  items = Parser.parse( text  )

  items.each_with_index do |item,i|
    ## add page_title
    ##   todo/fix:  check if title exists? do NOT overwrite - why? why not?

    page_meta = item[0]
    page_date = page_meta['date']

    page_title = ''
    if name.downcase == 'journal'  ## note: special case (do NOT auto-add journal to title)
      ## dont't add "default/generic" journal to title
    else
      page_title << "#{name} - "
    end
    page_title << "Day #{i+1}"
    page_title << " - #{page_date.strftime('%a, %-d %b')}"   if add_date

    page_meta['title'] = page_title
  end


  items.each_with_index do |item,i|
     page_meta    = item[0]
     page_content = item[1]

     page_date    = page_meta['date']
     page_title   = page_meta['title']

     path = ''
     path << "#{outpath}/#{page_date}"
     path << "-#{name.downcase}.md"      ## note: journal gets auto-added to the name too

     ##
     ##  check if path exits?
     page_root = File.dirname( File.expand_path( path ) )
     unless File.directory?( page_root )
       puts "   make (missing) output dirs >#{page_root}...<"
       FileUtils.makedirs( page_root )
     end


     puts "Writing entry #{i+1}/#{items.size} >#{page_title}< to #{path}..."

     ### todo:
     ##   add a comment in the yaml meta data block e.g.
     ##   #  Journal.TXT entry 1/4 - auto-built on xxxx by journaltxt v1.2.3
     ##   or something

     comment = "Journal.TXT entry #{i+1}/#{items.size} - auto-built on #{Time.now} by journaltxt/#{Journaltxt.version}"

     yaml_text = YAML.dump( page_meta )
     ## todo: check better way to add an upfront comment?
     ##   for now just replace leading --- with leading --- with comment
     yaml_text = yaml_text.sub( /^---[ ]*$\n?/, "---\n# #{comment}\n" )

     File.open( path, 'w:utf-8' ) do |f|
       f.write yaml_text
       f.write "---\n\n"
       f.write page_content
     end
  end
end

.build_file(path, opts = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/journaltxt.rb', line 79

def self.build_file( path, opts={} )
  text = File.open( path, 'r:bom|utf-8' ).read

  ## note: remove .txt extension if present for basename
  basename = File.basename( path, '.txt' )
  puts "basename:"
  pp basename

  ## note: only overwrite if NOT user-supplied
  opts[:name] ||= basename

  build( text, opts )
end

.mainObject



21
22
23
# File 'lib/journaltxt.rb', line 21

def self.main
  process( ARGV )
end

.process(args) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/journaltxt.rb', line 32

def self.process( args )

  config = {}   ## note: keep use supplied and defaults separated!!! entry nil - if not set!!

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: journaltxt [OPTS]"

    opts.on("-v", "--[no-]verbose", "Show debug messages") do |verbose|
      config[:verbose] = verbose
    end

    ## use --outdir or outputdir or something or output
    opts.on("-o", "--output=PATH", "Output path (default: #{DEFAULTS[:outpath]})") do |outpath|
      config[:outpath] = outpath
    end

    opts.on("-n", "--name=NAME", "Journal name (default: #{DEFAULTS[:name]})") do |name|
      config[:name] = name
    end

    opts.on("--[no-]date", "Add date to page title (default: #{DEFAULTS[:date]})") do |date|
      config[:date] = date
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  parser.parse!(args)

  puts ":: Config :::"
  pp config
  puts ":: Args :::"
  pp args

  if args.size == 0   ## default to journal.txt  if no filename passed along
    args << 'journal.txt'
  end

  args.each do |arg|
    build_file( arg, config )
  end
end

.rootObject



18
19
20
# File 'lib/journaltxt/version.rb', line 18

def self.root
  "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
end

.versionObject



10
11
12
# File 'lib/journaltxt/version.rb', line 10

def self.version
  VERSION
end