Module: Textus::Format
- Defined in:
- lib/textus/format.rb,
lib/textus/format/base.rb,
lib/textus/format/json.rb,
lib/textus/format/text.rb,
lib/textus/format/yaml.rb,
lib/textus/format/markdown.rb
Defined Under Namespace
Classes: Base, Json, Markdown, Text, Yaml
Constant Summary
collapse
- SEP =
"---".freeze
- STRATEGIES =
{
"markdown" => -> { Format::Markdown },
"json" => -> { Format::Json },
"yaml" => -> { Format::Yaml },
"text" => -> { Format::Text },
}.freeze
- EXT_TO_FORMAT =
{
".md" => "markdown",
".json" => "json",
".yaml" => "yaml",
".yml" => "yaml",
".txt" => "text",
}.freeze
Class Method Summary
collapse
Class Method Details
.data_to_payload(data, format) ⇒ Object
55
56
57
58
59
|
# File 'lib/textus/format.rb', line 55
def self.data_to_payload(data, format)
return { meta: {}, body: "", content: nil } if data.nil?
Format.for(format).data_to_payload(data)
end
|
.for(format) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/textus/format.rb', line 32
def self.for(format)
key = format.to_s
return registry.fetch(key).call if registry&.key?(key)
STRATEGIES.fetch(key) { raise Textus::UsageError.new("unknown entry format: #{format.inspect}") }.call
end
|
43
44
45
|
# File 'lib/textus/format.rb', line 43
def self.formats
EXT_TO_FORMAT.values.uniq
end
|
.infer_from_extension(ext) ⇒ Object
39
40
41
|
# File 'lib/textus/format.rb', line 39
def self.infer_from_extension(ext)
EXT_TO_FORMAT[ext]
end
|
.parse(raw, path: nil, format: "markdown") ⇒ Object
47
48
49
|
# File 'lib/textus/format.rb', line 47
def self.parse(raw, path: nil, format: "markdown")
Format.for(format).parse(raw, path: path)
end
|
.registry ⇒ Object
20
21
22
|
# File 'lib/textus/format.rb', line 20
def self.registry
@registry
end
|
.registry=(reg) ⇒ Object
16
17
18
|
# File 'lib/textus/format.rb', line 16
def self.registry=(reg)
@registry = reg
end
|
.serialize(meta: {}, body: "", content: nil, format: "markdown") ⇒ Object
51
52
53
|
# File 'lib/textus/format.rb', line 51
def self.serialize(meta: {}, body: "", content: nil, format: "markdown")
Format.for(format).serialize(meta: meta, body: body, content: content)
end
|