forked from mina-deploy/mina-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
23 changed files
with
359 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
gem "middleman", "3.0.0.rc.1" | ||
gem "scribe", path: './scribe' | ||
gem "mmdoc", path: './mmdoc' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
module Mmdoc | ||
require 'mmdoc/extension' | ||
|
||
autoload :Page, 'mmdoc/page' | ||
autoload :Pages, 'mmdoc/pages' | ||
autoload :PageHelpers, 'mmdoc/page_helpers' | ||
autoload :MiscHelpers, 'mmdoc/misc_helpers' | ||
autoload :IndexBuilder, 'mmdoc/index_builder' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'middleman-core' | ||
|
||
module Mmdoc | ||
module Extension | ||
class << self | ||
# Root path | ||
def root(*args) | ||
File.join File.dirname(__FILE__), *args | ||
end | ||
|
||
def registered(app, options={}) | ||
app.helpers PageHelpers | ||
app.helpers MiscHelpers | ||
end | ||
end | ||
end | ||
end | ||
|
||
::Middleman::Extensions.register(:mmdoc) { ::Mmdoc::Extension } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'json' | ||
|
||
module Mmdoc | ||
class IndexBuilder | ||
STOPWORDS = %w(a an and are as at be but by for if in into is) + | ||
%w(it no not of on or s such t that the their then) + | ||
%w(there these they this to was will with) + | ||
%w(class method return end if else) | ||
|
||
def initialize(pages) | ||
@pages = pages | ||
end | ||
|
||
def pages_index | ||
@pages.inject([]) { |h, p| | ||
h << { | ||
:title => p.to_s, | ||
:url => p.path, | ||
:type => p.data['group'], | ||
:parent => (urls.index(p.parent.path) if p.parent?) } | ||
h | ||
} | ||
end | ||
|
||
def urls | ||
@urls ||= @pages.map { |p| p.path } | ||
end | ||
|
||
def search_index | ||
# Build a search index (hash of hashes of words and page indices) | ||
search_index = Hash.new { |hash, k| hash[k] = Hash.new { |hh, kk| hh[kk] = 0 } } | ||
|
||
@pages.each do |p| | ||
i = urls.index(p.path) | ||
|
||
if p.to_s.count(' ') > 0 | ||
# Partial title match | ||
fuzzy_words(p).each { |word| search_index[word][i] += 30 } | ||
else | ||
# Exact title match | ||
fuzzy_words(p).each { |word| search_index[word][i] += 60 } | ||
end | ||
|
||
# Fuzzy title match | ||
fuzzy_words(p).each { |word| search_index[word][i] += 3 } | ||
|
||
# Content match | ||
fuzzy_words(p.content).each { |word| search_index[word][i] += 1 } | ||
end | ||
|
||
search_index | ||
end | ||
|
||
def indices | ||
{ | ||
:pages => pages_index, | ||
:search => search_index | ||
} | ||
end | ||
|
||
private | ||
|
||
# "Hello world" => ["hello", "world" ] | ||
def words(str) | ||
str.to_s.downcase.scan(/[A-Za-z0-9\_]+/) | ||
end | ||
|
||
# "hello" => ["he", "hel", "hell", "hello" ] | ||
def fuzzies(str) | ||
str = str.to_s.downcase; (1...str.size).map { |n| str[0..n] } | ||
end | ||
|
||
# "hello world" => ["he", "hel", "hell", "hello", "wo", "wor", "worl", "world" ] | ||
def fuzzy_words(str) | ||
words(str).map { |word| fuzzies(word) if word.size > 2 and !STOPWORDS.include?(word) }.compact.flatten | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Mmdoc | ||
module MiscHelpers | ||
# Relativize a path | ||
def rel(url) | ||
# Assume abs path | ||
url = url[1..-1] | ||
url = url.squeeze('/') | ||
|
||
# Append ../'s | ||
depth = request.path.count('/') - 1 | ||
path = '../' * depth + url | ||
|
||
path.squeeze('/') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
module Mmdoc | ||
# Page model. | ||
# | ||
# # Lookup by filename | ||
# Page['index.html.haml'] #=> Page instance or nil | ||
# Page.glob 'index.html.*' #=> Array of Pages | ||
# | ||
# | ||
# Metadata: | ||
# | ||
# page.title #=> "Getting started" | ||
# page.basename #=> "getting_started" | ||
# page.path #=> "/getting_started/index.html" | ||
# page.data #=> Hash | ||
# page.template_type #=> "haml" | ||
# page.raw #=> String (raw template data) | ||
# page.content #=> String | ||
# | ||
# Tree traversal: | ||
# | ||
# page.parent? #=> True/false | ||
# page.parent #=> Page, or nil if it's a root | ||
# page.root? #=> True if depth = 0 | ||
# page.depth #=> Number, minimum of 0 | ||
# page.breadcrumbs #=> Array of ancestors | ||
# page.children #=> Array | ||
# page.siblings #=> Array | ||
# | ||
class Page | ||
attr_reader :path | ||
attr_reader :basename | ||
attr_reader :raw | ||
attr_reader :title | ||
attr_reader :data | ||
attr_reader :template_type | ||
|
||
def self.source_path | ||
'source' | ||
end | ||
|
||
def self.glob(spec, except=nil) | ||
fullspec = File.join(source_path, spec) | ||
list = Dir[fullspec].map do |f| | ||
Page[ f[(source_path.length + 1)..-1] ] | ||
end.sort | ||
list = list.reject { |p| p.path == except.path } if except | ||
list | ||
end | ||
|
||
def self.[](name) | ||
@pages ||= Hash.new | ||
@pages[name] ||= Page.new name | ||
end | ||
|
||
def self.roots | ||
Pages.new(glob('*.html.*') + glob('*/index.html.*')) | ||
end | ||
|
||
def self.all | ||
list = Array.new | ||
work = lambda { |page| | ||
list << page | ||
page.children.each { |subpage| work[subpage] } | ||
} | ||
roots.each { |page| work[page] } | ||
|
||
Pages.new list | ||
end | ||
|
||
def sort_index | ||
[ (data['order'] || 99999), @basename ] | ||
end | ||
|
||
def <=>(other) | ||
sort_index <=> other.sort_index | ||
end | ||
|
||
def self.mm_server | ||
::Middleman::Application.server.inst | ||
end | ||
|
||
def self.fm_manager | ||
mm_server.frontmatter_manager | ||
end | ||
|
||
def initialize(path) | ||
path = path.gsub(/^(\.?\/)+/, '') | ||
@basepath = (path =~ /^(.*)\.html\.([A-Za-z0-9]*)$/) && $1 || path | ||
@template_type = $2 | ||
@basename = File.basename(@basepath) | ||
@dir = File.dirname(path) | ||
@parent_dir = File.dirname(@dir) | ||
@parent_dir = '' if @parent_dir == '.' | ||
@full_path = File.join(self.class.source_path, path) | ||
@path = "/#{@basepath}.html" | ||
@data, @raw = self.class.fm_manager.data(path) | ||
@title = data['title'] || @basename | ||
end | ||
|
||
def content | ||
# TODO: Parse | ||
@content ||= @raw | ||
end | ||
|
||
def to_s | ||
title | ||
end | ||
|
||
def parent | ||
if @basename == 'index' | ||
# ./tasks/index.html is a child of ./index.html | ||
Page.glob(File.join(@parent_dir, 'index.html.*'), self).first | ||
else | ||
# ./tasks/git_clone.html is a child of ./tasks/index.html, | ||
# or ./tasks.html | ||
Page.glob(File.join(@dir, 'index.html.*'), self).first || | ||
Page.glob(File.join(@parent_dir, "#{File.basename(@dir)}.html.*"), self).first | ||
end | ||
end | ||
|
||
def parent? | ||
!! parent | ||
end | ||
|
||
def root? | ||
! parent? | ||
end | ||
|
||
def depth | ||
root ? 0 : parent.depth + 1 | ||
end | ||
|
||
def breadcrumbs | ||
if parent? | ||
[ *parent.breadcrumbs, self ] | ||
else | ||
[ self ] | ||
end | ||
end | ||
|
||
def children | ||
list = if @basename == 'index' && !root? | ||
(Page.glob(File.join(@dir, '*.html.*'), self) + | ||
Page.glob(File.join(@dir, '*', 'index.html.*'), self)).sort | ||
else | ||
(Page.glob(File.join(@dir, @basepath, '*.html.*'), self) + | ||
Page.glob(File.join(@dir, @basepath, '*', 'index.html.*'), self)).sort | ||
end | ||
|
||
Pages.new list | ||
end | ||
|
||
def siblings | ||
if @basename == 'index' && !root? | ||
list = (Page.glob(File.join(@parent_dir, '*.html.*')) + | ||
Page.glob(File.join(@parent_dir, '*', 'index.html.*'))).sort | ||
else | ||
list = Page.glob(File.join(@dir, '*.html.*')) | ||
list = list.reject { |p| p.basename == 'index' } | ||
list += Page.glob(File.join(@dir, '*', 'index.html.*')) | ||
list = list.sort | ||
end | ||
Pages.new list | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module Mmdoc | ||
module PageHelpers | ||
# Returns the current page (a Page instance). | ||
def here | ||
Page.glob(request.path + '.*').first | ||
end | ||
|
||
def all_pages | ||
Page.all | ||
end | ||
|
||
def roots | ||
Page.roots | ||
end | ||
|
||
# Returns the main page. | ||
def index | ||
Page.glob('index.html.*').first | ||
end | ||
|
||
# Returns the name of the site. | ||
def site_name | ||
index ? index.title : "Site" | ||
end | ||
|
||
# Returns groups for the side nav. | ||
# Returns a hash with keys as group names, values as Page arrays (Pages). | ||
def nav_groups | ||
children = here.children.any? ? here.children : here.siblings | ||
groups = children.groups | ||
groups[here.title] = groups.delete("") if groups[""] | ||
groups | ||
end | ||
|
||
# Breadcrumbs for side nav. | ||
def nav_breadcrumbs | ||
parent = (here.children.any? ? here : here.parent) || here | ||
parent.breadcrumbs | ||
end | ||
|
||
# Returns a CSS class name for a Page. | ||
def item_class(page) | ||
[ | ||
('active' if page.path == here.path), | ||
('more' if page.children.any?) | ||
].compact.join(' ') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Mmdoc | ||
class Pages < ::Array | ||
def groups(attribute='group') | ||
group_by { |p| p.data[attribute.to_s] } | ||
end | ||
|
||
def indices | ||
i = IndexBuilder.new(self) | ||
i.indices | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
//= require scribe-search | ||
//= require scribe-pre | ||
//= require search_index | ||
//= require mmdoc-search | ||
//= require mmdoc-pre | ||
//= require mmdoc-index |