Sha256: f1b105f3a1cc143ce55cb5edc4664e50a963b3f940b996f6d7f09c5e425952ca

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

class DocFile
  DEFAULT_IMAGE_PATH = "/docs/img"

  def self.find(basename:, track_dir:)
    dir = File.join(track_dir, "docs")

    [
      MarkdownFile.new(basename: basename, docs_dir: dir),
      OrgmodeFile.new(basename: basename, docs_dir: dir),
    ].detect(&:exist?) || NullFile.new(basename)
  end

  attr_reader :basename, :dir
  def initialize(basename:, docs_dir:)
    @basename = basename
    @dir = docs_dir
  end

  def render(image_path: DEFAULT_IMAGE_PATH)
    body.gsub(img_src, img_dst(image_path))
  end

  def name
    "%s.%s" % [basename, extension]
  end

  def extension
    "md"
  end

  def exist?
    File.exist?(path)
  end

  private

  def body
    File.read(path)
  end

  def path
    "%s/%s" % [dir, name]
  end

  def img_src
    Regexp.new("]\\(%s" % DEFAULT_IMAGE_PATH)
  end

  def img_dst(image_path)
    "](%s" % image_path.gsub(Regexp.new("/$"), "")
  end
end

class OrgmodeFile < DocFile
  def body
    Orgmode::Parser.new(File.read(path)).to_markdown
  end

  def extension
    "org"
  end
end

class MarkdownFile < DocFile
end

class NullFile
  attr_reader :basename
  def initialize(basename)
    @basename = basename
  end

  def render(image_path:"")
    ""
  end

  def name
    "%s.%s" % [basename, extension]
  end

  def extension
    "md" # default for missing files
  end

  def exist?
    false
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trackler-2.1.0.42 lib/trackler/doc_file.rb