class Marsdawn::Source

Attributes

doc_info[R]
local2uri[R]

Public Class Methods

new(path) click to toggle source
# File lib/marsdawn/source.rb, line 10
def initialize path
  @path = File.expand_path(path)
  raise "No source directory '#{@path}'." unless File.exists?(@path)
  @doc_info = {
    key: nil,
    lang: 'en',
    title: '',
    version: '0.0.0',
    markdown_extname: '.md',
    directory_index: '.index.md',
    encoding: 'utf-8',
    link_defs: {},
    kramdown_options: {}
  }
  load_doc_info
  investigate
end

Public Instance Methods

each_contents(options) { |uri, markdown(file, uri, options), front_matter, sysinfo| ... } click to toggle source
# File lib/marsdawn/source.rb, line 28
def each_contents options
  @local2uri.each do |file, uri|
    yield uri, markdown(file, uri, options), @front_matter[uri], @sysinfo[uri]
  end
end

Protected Instance Methods

create_breadcrumb(uri) click to toggle source
# File lib/marsdawn/source.rb, line 150
def create_breadcrumb uri
  uri.split('/').each_with_object([]) do |dir, ret|
    parent = (ret.last == '/' ? '' : ret.last)
    path = "#{parent}/#{dir}"
    ret << path unless uri == path
  end
end
create_site_index() click to toggle source
# File lib/marsdawn/source.rb, line 158
def create_site_index
  @front_matter.each_with_object({}) do |(uri, vars), ret|
    ret[uri] = vars[:title]
  end
end
digg(path, uri='') click to toggle source
# File lib/marsdawn/source.rb, line 89
def digg path, uri=''
  items = Dir.glob(File.join(path,'*')).map{|f| File.basename(f)}.sort
  read_directory_index path, uri
  items.each do |item|
    fullpath = File.join(path, item)
    uri_item = (item =~ /^\d+_(.*)/ ? $1 : item)
    if File.directory?(fullpath)
      digg fullpath, "#{uri}/#{uri_item}"
    elsif uri_item != @doc_info[:directory_index]
      set_page_info fullpath, uri, uri_item
    end
  end
end
investigate() click to toggle source
# File lib/marsdawn/source.rb, line 44
def investigate
  @local2uri = {}
  @front_matter = {}
  @sysinfo = {}
  digg @path
  update_sysinfo
  update_docinfo
end
load_doc_info() click to toggle source
# File lib/marsdawn/source.rb, line 35
def load_doc_info
  doc_info_file = File.join(@path, '.marsdawn.yml')
  raise "There is no doc_info file '.marsdawn.yml' in '#{@path}'" unless File.exists?(doc_info_file)
  conf = Marsdawn::Util.hash_symbolize_keys(YAML.load_file(doc_info_file))
  @doc_info.merge! conf
  @doc_key = @doc_info[:key]
  raise "The document key should be specified in .marsdawn.yml." if @doc_key.nil? || @doc_key.empty?
end
markdown(file, uri, opts) click to toggle source
# File lib/marsdawn/source.rb, line 84
def markdown file, uri, opts
  opts[:link_defs] = link_defs(uri)
  Document.read(file, @front_matter[uri][:title], opts).to_html
end
read_directory_index(path, uri) click to toggle source
# File lib/marsdawn/source.rb, line 114
def read_directory_index path, uri
  indexfile = File.join(path, @doc_info[:directory_index])
  if File.exists?(indexfile)
    uri = (uri == '' ? '/' : uri)
    @local2uri[indexfile] = uri
    @front_matter[uri] = read_front_matter(indexfile, File.basename(uri))
    @sysinfo[uri] = {:type => 'page'}
  end
end
read_front_matter(file, name) click to toggle source
# File lib/marsdawn/source.rb, line 124
def read_front_matter file, name
  doc = Document.read(file, name, @doc_info[:kramdown_options])
  doc.front_matter
end
set_page_info(fullpath, uri, uri_item) click to toggle source
# File lib/marsdawn/source.rb, line 103
def set_page_info fullpath, uri, uri_item
  extname = File.extname(uri_item)
  if extname == @doc_info[:markdown_extname]
    uri_item = File.basename(uri_item, extname)
    fulluri = "#{uri}/#{uri_item}"
    @local2uri[fullpath] = fulluri
    @front_matter[fulluri] = read_front_matter(fullpath, uri_item)
    @sysinfo[fulluri] = {:type => 'folder'}
  end
end
update_docinfo() click to toggle source
# File lib/marsdawn/source.rb, line 146
def update_docinfo
  @doc_info[:site_index] = create_site_index
end
update_sysinfo() click to toggle source
# File lib/marsdawn/source.rb, line 129
def update_sysinfo
  prev_page = nil
  @sysinfo.each do |uri, info|
    @sysinfo[uri][:uri] = uri
    @sysinfo[uri][:level] = uri.count('/')
    @sysinfo[uri][:breadcrumb] = create_breadcrumb(uri)
    @sysinfo[uri][:parent] = @sysinfo[uri][:breadcrumb].last
    @sysinfo[uri][:prev_page] = nil
    @sysinfo[uri][:next_page] = nil
    unless prev_page.nil?
      @sysinfo[uri][:prev_page] = prev_page
      @sysinfo[prev_page][:next_page] = uri
    end
    prev_page = uri
  end
end