Class: Textus::Format::Markdown
- Inherits:
-
Base
- Object
- Base
- Textus::Format::Markdown
show all
- Defined in:
- lib/textus/format/markdown.rb
Class Method Summary
collapse
Methods inherited from Base
enforce_name_match!, format_name, rewrite_name, validate_against, validate_raw_entry!
Class Method Details
.data_to_payload(data) ⇒ Object
53
54
55
56
|
# File 'lib/textus/format/markdown.rb', line 53
def self.data_to_payload(data)
data = data.transform_keys(&:to_s) if data.is_a?(Hash)
{ meta: data["_meta"] || {}, body: (data["body"] || "").to_s, content: nil }
end
|
.extensions ⇒ Object
35
|
# File 'lib/textus/format/markdown.rb', line 35
def self.extensions = [".md"]
|
.nested_glob ⇒ Object
37
|
# File 'lib/textus/format/markdown.rb', line 37
def self.nested_glob = "**/*.md"
|
.parse(raw, path: nil) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/textus/format/markdown.rb', line 6
def self.parse(raw, path: nil)
raw = raw.dup.force_encoding(Encoding::UTF_8)
raise BadFrontmatter.new(path, "entry is not valid UTF-8") unless raw.valid_encoding?
return { "_meta" => {}, "body" => raw, "content" => nil } unless raw.start_with?("---\n") || raw.start_with?("---\r\n")
lines = raw.split(/\r?\n/, -1)
close_idx = lines[1..].index("---")
raise BadFrontmatter.new(path, "frontmatter not terminated") unless close_idx
close_idx += 1
fm_yaml = lines[1...close_idx].join("\n")
body = lines[(close_idx + 1)..].join("\n")
begin
fm = fm_yaml.strip.empty? ? {} : ::YAML.safe_load(fm_yaml, permitted_classes: [Date, Time], aliases: false)
rescue Psych::SyntaxError => e
raise BadFrontmatter.new(path, "YAML parse failed: #{e.message}")
end
fm = {} unless fm.is_a?(Hash)
{ "_meta" => fm, "body" => body, "content" => nil }
end
|
.serialize(meta:, body:, content: nil) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/textus/format/markdown.rb', line 27
def self.serialize(meta:, body:, content: nil)
_ = content
fm_yaml = meta.empty? ? "" : ::YAML.dump(meta).sub(/\A---\n/, "")
body = body.to_s
body += "\n" unless body.empty? || body.end_with?("\n")
"---\n#{fm_yaml}---\n#{body}"
end
|
.serialize_for_put(meta:, body:, content:, path:) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/textus/format/markdown.rb', line 39
def self.serialize_for_put(meta:, body:, content:, path:)
_ = path
_ = content
bytes = serialize(meta: meta || {}, body: body.to_s)
[bytes, meta, body.to_s, nil]
end
|
.validate_path_extension(path, _nested) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/textus/format/markdown.rb', line 46
def self.validate_path_extension(path, _nested)
ext = File.extname(path)
return if ["", ".md"].include?(ext)
raise UsageError.new("markdown format requires '.md' path (got #{ext.inspect})")
end
|