Module: Juli::Util

Defined Under Namespace

Classes: Config, JuliI18n, Repo

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.confObject

return REPO/config hash



156
157
158
# File 'lib/juli/util.rb', line 156

def conf
  Config.instance.conf
end

.find_template(template, t_opt = nil) ⇒ Object

find erb template under the following order:

if t_opt (‘-t’ command-line option arg) is specified:

1st) template_path in absolute or relative from current dir, or
2nd) -t template_path in JULI_REPO/.juli/, or
3rd) -t template_path in lib/juli/template/
otherwise, error

else:

4th) {template} in JULI_REPO/.juli/, or
5th) {template} in lib/juli/template.
otherwise, error

Where, template means conf

INPUTS

template

template name

t_opt

template name which -t command-line option specifies



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/juli/util.rb', line 231

def find_template(template, t_opt = nil)
  if t_opt
    if File.exist?(t_opt)
      t_opt
    else
      find_template_sub(t_opt)
    end
  else
    find_template_sub(template)
  end
end

.in_filename(out_filename) ⇒ Object

INPUTS

out_filename

relative path under OUTPUT_TOP

RETURN

relative path of in-filename, but **no extention**.

EXAMPLE

diary/2010/12/31.shtml -> 31



208
209
210
211
# File 'lib/juli/util.rb', line 208

def in_filename(out_filename)
  File.join(File.dirname(out_filename),
            File.basename(out_filename).gsub(/\.[^\.]*$/,''))
end

.juli_repo(path = '.') ⇒ Object

fullpath of juli-repository

it is enough to have one value in whole juli modules so SINGLETON-pattern is used.



123
124
125
126
# File 'lib/juli/util.rb', line 123

def juli_repo(path='.')
  $_repo ||= Repo.new(path)
  $_repo.juli_repo
end

.mkdir(path) ⇒ Object

mkdir for out_file if necessary



162
163
164
165
166
167
# File 'lib/juli/util.rb', line 162

def mkdir(path)
  dir = File.dirname(path)
  if !File.directory?(dir)
    FileUtils.mkdir_p(dir)
  end
end

.out_filename(in_filename, o_opt = nil) ⇒ Object

INPUTS

in_filename

relative path under repository

o_opt

output path which -o command-line option specifies

RETURN

full path of out filename. if o_opt is specified, it is used.

EXAMPLE

diary/2010/12/31.txt -> OUTPUT_TOP/diary/2010/12/31.shtml



193
194
195
196
197
# File 'lib/juli/util.rb', line 193

def out_filename(in_filename, o_opt = nil)
  o_opt ||
      File.join(conf['output_top'],
                to_wikiname(in_filename) + conf['ext'])
end

.str_limit(str) ⇒ Object



80
81
82
# File 'lib/juli/util.rb', line 80

def str_limit(str)
  str.size > 45 ? str[0..45] + '...' : str
end

.str_trim(str) ⇒ Object

trim string just for printing purpose here



86
87
88
# File 'lib/juli/util.rb', line 86

def str_trim(str)
  str_limit(str.gsub(/\n/, '\n').gsub(/\r/, '\r'))
end

.to_wikiname(in_filename) ⇒ Object

input filename to wikiname

INPUTS

in_filename

juli repo file path

EXAMPLE

diary/2010/12/31.txt -> diary/2010/12/31



177
178
179
# File 'lib/juli/util.rb', line 177

def to_wikiname(in_filename)
  in_filename.gsub(/\.[^\.]*$/,'')
end

.underscore(str) ⇒ Object

Similar to Rails underscore() method.

Example: ‘A::B::HelperMethod’ -> ‘helper_method’



94
95
96
97
98
99
100
# File 'lib/juli/util.rb', line 94

def underscore(str)
  str.gsub(/.*::/,'').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end

.usageObject



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
77
# File 'lib/juli/util.rb', line 34

def usage
  <<EOM
USAGE: juli [general_options] COMMAND [command_options] [files]

general_options:
  --help
  --version

COMMAND (default = gen):
  init                initialize current directory as juli-repo
  gen                 generate outputs from files under juli-repo
                  This is the default juli command.
  sitemap             generate sitemap to JULI_REPO/sitemap.shtml
  recent_update       generate reent updates to JULI_REPO/recent_update.shtml

                  NOTE: file extention '.shtml' above is the default.
                  you can change it by 'init' command -e option
                  (see below), or by modifying JULI_REPO/.juli/config
                  'ext' entry later anytime.

  tag                 generate tag-list page.  see tag(macro) manual.

command_options for:
  init:
-o output_top     default='../html/'
-t template       set the template at config (default='default.html').
                  This template name will be used at 'gen' command
                  (described below) to search 1) JULI_REPO/.juli/ or
                  2) lib/juli/template/
-e ext            generating html file extention (default='.shtml')

  gen:
-g generator      specify generator as follows(default=html):
                     #{visitor_list.join("\n" + " "*25)}
-f                force generate
-t template_path  use the template path rather than juli-config value
                  set at 'juli init -t ...'
-o output_path    specify output file path.  It cannot be set at bulk-mode.
                  default is under the directory defined at .juli/config
                  'output_top' entry.

Where, JULI_REPO is the directory which 'juli init' is executed.
EOM
end

.visitor(str) ⇒ Object

Visitor::#str constantize



13
14
15
16
17
18
19
20
# File 'lib/juli/util.rb', line 13

def visitor(str)
  camelized = camelize(str)
  if Visitor.const_defined?(camelized)
    Visitor.const_get(camelize(str))
  else
    raise "Visitor #{camelized} is not defined."
  end
end

.visitor_listObject



23
24
25
26
27
28
29
30
31
# File 'lib/juli/util.rb', line 23

def visitor_list
  result = []
  sorted_visitors = Dir.glob(File.join(Juli::LIB, 'visitor/*.rb')).sort
  for f in sorted_visitors do
    next if f =~ /^\./
    result << File.basename(f).gsub(/\.rb$/, '')
  end
  result
end

Instance Method Details

#camelize(str) ⇒ Object



8
9
10
# File 'lib/juli/util.rb', line 8

def camelize(str)
  str.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end