Module: Textus::Protocol::Format

Defined in:
lib/textus/protocol/format.rb,
lib/textus/protocol/format/base.rb,
lib/textus/protocol/format/bash.rb,
lib/textus/protocol/format/json.rb,
lib/textus/protocol/format/ruby.rb,
lib/textus/protocol/format/text.rb,
lib/textus/protocol/format/yaml.rb,
lib/textus/protocol/format/python.rb,
lib/textus/protocol/format/script.rb,
lib/textus/protocol/format/markdown.rb,
lib/textus/protocol/format/javascript.rb

Defined Under Namespace

Classes: Base, Bash, Javascript, Json, Markdown, Python, Ruby, Script, Text, Yaml

Constant Summary collapse

SEP =
"---".freeze
STRATEGIES =
{
  "markdown" => -> { Protocol::Format::Markdown },
  "json" => -> { Protocol::Format::Json },
  "yaml" => -> { Protocol::Format::Yaml },
  "text" => -> { Protocol::Format::Text },
  "script" => -> { Protocol::Format::Script },
  "bash" => -> { Protocol::Format::Bash },
  "ruby" => -> { Protocol::Format::Ruby },
  "python" => -> { Protocol::Format::Python },
  "javascript" => -> { Protocol::Format::Javascript },
}.freeze
EXT_TO_FORMAT =
{
  ".md" => "markdown",
  ".json" => "json",
  ".yaml" => "yaml",
  ".yml" => "yaml",
  ".txt" => "text",
  ".rb" => "ruby",
  ".py" => "python",
  ".sh" => "bash",
  ".js" => "javascript",
  ".pl" => "script",
  ".rs" => "script",
  ".exs" => "script",
}.freeze

Class Method Summary collapse

Class Method Details

.data_to_payload(data, format) ⇒ Object



66
67
68
69
70
# File 'lib/textus/protocol/format.rb', line 66

def self.data_to_payload(data, format)
  return { meta: {}, body: "", content: nil } if data.nil?

  Protocol::Format.for(format).data_to_payload(data)
end

.for(format) ⇒ Object



43
44
45
46
47
48
# File 'lib/textus/protocol/format.rb', line 43

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

.formatsObject



54
55
56
# File 'lib/textus/protocol/format.rb', line 54

def self.formats
  EXT_TO_FORMAT.values.uniq
end

.infer_from_extension(ext) ⇒ Object



50
51
52
# File 'lib/textus/protocol/format.rb', line 50

def self.infer_from_extension(ext)
  EXT_TO_FORMAT[ext]
end

.parse(raw, path: nil, format: "markdown") ⇒ Object



58
59
60
# File 'lib/textus/protocol/format.rb', line 58

def self.parse(raw, path: nil, format: "markdown")
  Protocol::Format.for(format).parse(raw, path: path)
end

.registryObject



24
25
26
# File 'lib/textus/protocol/format.rb', line 24

def self.registry
  @registry
end

.registry=(reg) ⇒ Object



20
21
22
# File 'lib/textus/protocol/format.rb', line 20

def self.registry=(reg)
  @registry = reg
end

.serialize(meta: {}, body: "", content: nil, format: "markdown") ⇒ Object



62
63
64
# File 'lib/textus/protocol/format.rb', line 62

def self.serialize(meta: {}, body: "", content: nil, format: "markdown")
  Protocol::Format.for(format).serialize(meta: meta, body: body, content: content)
end