Module: PBSimply::Frontmatter
- Included in:
- PBSimply
- Defined in:
- lib/pbsimply/frontmatter.rb
Instance Method Summary collapse
-
#read_frontmatter(dir, filename) ⇒ Object
Read Frontmatter from the document.
Instance Method Details
#read_frontmatter(dir, filename) ⇒ Object
Read Frontmatter from the document. This method returns frontmatter, pos. pos means position at end of Frontmatter on the file.
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/pbsimply/frontmatter.rb', line 9 def read_frontmatter(dir, filename) frontmatter = nil pos = nil source_path = File.join(dir, filename) if File.exist? File.join(dir, ".meta." + filename) # Load standalone metadata YAML. frontmatter = Psych.unsafe_load(File.read(File.join(dir, (".meta." + filename)))) pos = 0 else case File.extname filename when ".md" # Load Markdown's YAML frontmatter. File.open(source_path) do |f| l = f.gets next unless l && l.chomp == "---" lines = [] while l = f.gets break if l.nil? break if l.chomp == "---" lines.push l end next if f.eof? begin frontmatter = Psych.unsafe_load(lines.join) rescue => e STDERR.puts "!CRITICAL: Cannot parse frontmatter." raise e end pos = f.pos end when ".rst" # ReSTRUCTURED Text File.open(source_path) do |f| l = f.gets if l =~ /:([A-Za-z_-]+): (.*)/ #docinfo frontmatter = { $1 => [$2.chomp] } last_key = $1 # Read docinfo while(l = f.gets) break if l =~ /^\s*$/ # End of docinfo if l =~ /^\s+/ # Continuous line docinfo_lines.last.push($'.chomp) elsif l =~ /:([A-Za-z_-]+): (.*)/ frontmatter[$1] = [$2.chomp] last_key = $1 end end # Treat docinfo lines frontmatter.each do |k,v| v = v.join(" ") #if((k == "author" || k == "authors") && v.include?(";")) # Multiple authors. if(v.include?(";")) # Multiple element. v = v.split(/\s*;\s*/) elsif k == "date" # Date? # Datetime? if v =~ /[0-2][0-9]:[0-6][0-9]/ v = Time.parse(v) else v = Date.parse(v) end elsif v == "yes" || v == "true" v = true else # Simple String. nil # keep v end frontmatter[k] = v end elsif l && l.chomp == ".." #YAML # Load ReST YAML that document begins comment and block is yaml. lines = [] while(l = f.gets) if(l !~ /^\s*$/ .. l =~ /^\s*$/) if l=~ /^\s*$/ break else lines.push l end end end next if f.eof? # Rescue for failed to read YAML. begin frontmatter = Psych.unsafe_load(lines.map {|i| i.sub(/^\s*/, "") }.join) rescue STDERR.puts "Error in parsing ReST YAML frontmatter (#{$!})" next end else next end pos = f.pos end end end abort "This document has no frontmatter" unless frontmatter abort "This document has no title." unless frontmatter["title"] outext = frontmatter["force_ext"] || ".html" outpath = case when @outfile @outfile when @accs_processing File.join(@config["outdir"], @dir, "index") + outext else File.join(@config["outdir"], @dir, File.basename(filename, ".*")) + outext end absolute_current = File.absolute_path Dir.pwd absolute_docdir = File.absolute_path dir absolute_docpath = File.absolute_path source_path pwd_length = absolute_current.length ### Additional meta values. ### frontmatter["source_directory"] = dir # Source Directory frontmatter["source_filename"] = filename # Source Filename frontmatter["source_path"] = source_path # Source Path frontmatter["dest_path"] = outpath frontmatter["normalized_docdir"] = absolute_docdir[pwd_length..] frontmatter["normalized_docpath"] = absolute_docpath[pwd_length..] # URL in site. this_url = (source_path).sub(/^[\.\/]*/) { @config["self_url_prefix"] || "/" }.sub(/\.[a-zA-Z0-9]+$/, ".html") frontmatter["page_url"] = this_url # URL in site with URI encode. frontmatter["page_url_encoded"] = ERB::Util.url_encode(this_url) frontmatter["page_url_encoded_external"] = ERB::Util.url_encode((source_path).sub(/^[\.\/]*/) { @config["self_url_external_prefix"] || "/" }.sub(/\.[a-zA-Z0-9]+$/, ".html")) frontmatter["page_html_escaped"] = ERB::Util.html_escape(this_url) frontmatter["page_html_escaped_external"] = ERB::Util.html_escape((source_path).sub(/^[\.\/]*/) { @config["self_url_external_prefix"] || "/" }.sub(/\.[a-zA-Z0-9]+$/, ".html")) # Title with URL Encoded. frontmatter["title_encoded"] = ERB::Util.url_encode(frontmatter["title"]) frontmatter["title_html_escaped"] = ERB::Util.html_escape(frontmatter["title"]) fts = frontmatter["timestamp"] fts = fts.to_datetime if Time === fts if DateTime === fts frontmatter["timestamp_xmlschema"] = fts.xmlschema frontmatter["timestamp_jplocal"] = fts.strftime('%Y年%m月%d日 %H時%M分%S秒') frontmatter["timestamp_rubytimestr"] = fts.strftime('%a %b %d %H:%M:%S %Z %Y') frontmatter["timestamp_str"] = fts.strftime("%Y-%m-%d %H:%M:%S %Z") elsif Date === fts frontmatter["timestamp_xmlschema"] = fts.xmlschema frontmatter["timestamp_jplocal"] = fts.strftime('%Y年%m月%d日') frontmatter["timestamp_rubytimestr"] = fts.strftime('%a %b %d') frontmatter["timestamp_str"] = fts.strftime("%Y-%m-%d") elsif Date === frontmatter["Date"] fts = frontmatter["Date"] frontmatter["timestamp_xmlschema"] = fts.xmlschema frontmatter["timestamp_jplocal"] = fts.strftime('%Y年%m月%d日') frontmatter["timestamp_rubytimestr"] = fts.strftime('%a %b %d') frontmatter["timestamp_str"] = fts.strftime("%Y-%m-%d") end fsize = FileTest.size(source_path) mtime = File.mtime(source_path).to_i frontmatter["_filename"] ||= filename frontmatter["pagetype"] ||= "post" frontmatter["_size"] = fsize frontmatter["_mtime"] = mtime if File.extname(filename) == ".md" frontmatter["_docformat"] = "Markdown" elsif File.extname(filename) == ".rst" || File.extname(filename) == ".rest" frontmatter["_docformat"] = "ReST" elsif File.extname(filename) == ".rdoc" frontmatter["_docformat"] = "RDoc" end frontmatter["date"] ||= @now.strftime("%Y-%m-%d %H:%M:%S") return frontmatter, pos end |