Module: Mayuki

Defined in:
lib/mayuki.rb,
lib/mayuki/version.rb

Constant Summary collapse

VERSION =
"0.0.2"
DATE =
"2012-08-20"

Class Method Summary collapse

Class Method Details

.export_html(text, conf) ⇒ Object



163
164
165
166
# File 'lib/mayuki.rb', line 163

def self.export_html(text, conf)
  (conf["_pygments"] ? "<link href='_pygments.css' rel='stylesheet'>\n" : "") +
  "#{text}"
end

.export_html_article(text, conf) ⇒ Object



168
169
170
171
172
173
# File 'lib/mayuki.rb', line 168

def self.export_html_article(text, conf)
  (conf["_pygments"] ? "<link href='_pygments.css' rel='stylesheet'>\n" : "") +
  "<article>\n"\
  "#{text}"\
  "\n</article>"
end

.export_html_full(text, conf) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mayuki.rb', line 175

def self.export_html_full(text, conf)
  "<!DOCTYPE html>\n"\
  "<html>\n"\
  "<head>\n" +
  (conf["_pygments"] ? "<link href='_pygments.css' rel='stylesheet'>\n" : "") +
  "</head>\n"\
  "<body>\n<article>\n"\
  "#{text}"\
  "\n</article>\n</body>"\
  "\n</html>"
end

.mayuki(input_dir = ".") ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
# File 'lib/mayuki.rb', line 9

def self.mayuki(input_dir = ".")
  
  # Set default configuration
  conf_global = {
    "_output" => File.join(input_dir, "_output"),
    "_render" => ["liquid"],
    "_export" => [],
    "_pygments" => false
    }
  
  # Load global configuration
  Dir[File.join(input_dir, "_conf*/*"),
    File.join(input_dir, "_conf*.yaml"),
    File.join(input_dir, "_conf*.yml")
    ].each do |f|
      begin
        conf_global.merge!(YAML.load_file(f))
        rescue
      end
    end
  
  # Prepare global output directory
  Dir.mkdir(conf_global["_output"]) unless Dir.exist?(conf_global["_output"])
  
  # Recurse each directory
  Dir[File.join(input_dir, "/"), File.join(input_dir, "**/*/")].each do |d|
    
    # Strip relative path
    rel = d.split(File.join(input_dir, "/"))[1]
    rel = "." unless rel
    
    # Skip if the directory is intended to be ignored
    next if not d.split("/").reduce(true) { |result, elem| result and elem[0] != "_" }
    
    # Get global configuration
    conf = Hash[conf_global]
    
    # Load local configuration
    Dir[File.join(d, "_conf*/*"),
      File.join(d, "_conf*.yaml"),
      File.join(d, "_conf*.yml")
      ].each do |f|
      begin
        conf.merge!(YAML.load_file(f))
        rescue
      end
    end
    
    # Prepare local output directory
    Dir.mkdir(conf["_output"]) unless Dir.exist?(conf["_output"])
    Dir.mkdir(File.join(conf["_output"], rel)) unless Dir.exist?(File.join(conf["_output"], rel))
    
    # Process each file
    Dir[File.join(d, "*")].each do |f|
      
      # Skip if it is a directory
      next if not File.file?(f)
      
      # Skip if the file is intended to be ignored
      next if File.basename(f).start_with?("_")
      
      # Get local configuration
      conf_infile = Hash[conf]
      
      # Markdown / HTML file (parsing)
      if [".markdown", ".md"
        ].index(File.extname(f))
        
        # Read text and metadata
        part1, sep, part2 = IO.read(f).partition("\n---")
        if sep == ""
          text = part1
        else
          yaml, text = [part1, part2]
          
          # Load in-file configuration
          begin
            conf_infile.merge!(YAML.load(yaml))
            rescue
          end
        end
        
        # Set environment variables for filter module
        $dir_i = d
        $dir_o = File.join(conf_infile["_output"], rel)
        
        # Render text
        ["liquid", "markdown"].each do |r|
          if conf_infile["_render"].index(r)
            text = method("render_" + r).call(text)
          end
        end
        
        # Export text
        if conf_infile["_export"].index("html")
          
          text = export_html(text, conf_infile)
          IO.write(File.join(conf_infile["_output"], rel, File.basename(f, File.extname(f)) + ".html"), text)
          
        elsif conf_infile["_export"].index("html_article")
          
          text = export_html_article(text, conf_infile)
          IO.write(File.join(conf_infile["_output"], rel, File.basename(f, File.extname(f)) + ".html"), text)
          
        elsif conf_infile["_export"].index("html_full")
          
          text = export_html_full(text, conf_infile)
          IO.write(File.join(conf_infile["_output"], rel, File.basename(f, File.extname(f)) + ".html"), text)
          
        else
          
          text = yaml + "\n---\n" + text if sep != ""
          IO.write(File.join(conf_infile["_output"], rel, File.basename(f)), text)
          
        end
        
        # Export pygments stylesheet (_pygments.css)
        if conf_infile["_pygments"]
          IO.write(File.join(conf_infile["_output"], rel, "_pygments.css"),
            ".pygments td.linenos { background-color: #f0f0f0; padding-right: 10px; }\n"\
            ".pygments span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }\n"\
            ".pygments pre { line-height: 125%; }\n")
          
          system("pygmentize -f html -S default -a .pygments "\
            ">> #{File.join(conf_infile["_output"], rel, "_pygments.css")}")
        end
        
      # Other file (copying)
      else
        
        # Read text
        text = IO.read(f)
        
        # Export text
        IO.write(File.join(conf_infile["_output"], rel, File.basename(f)), text)
        
      end
      
    end
    
  end
  
  conf_global["_output"]
  
end

.render_liquid(text) ⇒ Object



155
156
157
# File 'lib/mayuki.rb', line 155

def self.render_liquid(text)
  Liquid::Template.parse(text).render({}, :filters => [LiquidFilter])
end

.render_markdown(text) ⇒ Object



159
160
161
# File 'lib/mayuki.rb', line 159

def self.render_markdown(text)
  RDiscount.new(text).to_html
end