Skip to content

Commit

Permalink
Formalize the mmdoc gem.
Browse files Browse the repository at this point in the history
rstacruz committed Jun 10, 2012
1 parent c30dad5 commit ca285ef
Showing 23 changed files with 359 additions and 348 deletions.
2 changes: 1 addition & 1 deletion Gemfile
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'
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ./scribe
remote: ./mmdoc
specs:
scribe (0.0.1)
mmdoc (0.0.1)
middleman-core (>= 3.0.0.rc.1)

GEM
@@ -110,4 +110,4 @@ PLATFORMS

DEPENDENCIES
middleman (= 3.0.0.rc.1)
scribe!
mmdoc!
4 changes: 1 addition & 3 deletions config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'scribe_extension'

activate :automatic_image_sizes
activate :relative_assets
activate :scribe
activate :mmdoc

set :layout, :'_templates/layout'
set :github, 'nadarei/mina'
10 changes: 10 additions & 0 deletions mmdoc/lib/mmdoc.rb
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
19 changes: 19 additions & 0 deletions mmdoc/lib/mmdoc/extension.rb
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 }
78 changes: 78 additions & 0 deletions mmdoc/lib/mmdoc/index_builder.rb
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
16 changes: 16 additions & 0 deletions mmdoc/lib/mmdoc/misc_helpers.rb
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
166 changes: 166 additions & 0 deletions mmdoc/lib/mmdoc/page.rb
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
49 changes: 49 additions & 0 deletions mmdoc/lib/mmdoc/page_helpers.rb
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
12 changes: 12 additions & 0 deletions mmdoc/lib/mmdoc/pages.rb
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
2 changes: 1 addition & 1 deletion scribe/scribe.gemspec → mmdoc/mmdoc.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Gem::Specification.new do |s|
s.name = "scribe"
s.name = "mmdoc"
s.version = "0.0.1"
s.summary = ""
s.description = ""
File renamed without changes.
77 changes: 0 additions & 77 deletions scribe/lib/index_builder.rb

This file was deleted.

1 change: 0 additions & 1 deletion scribe/lib/middleman_extension.rb

This file was deleted.

14 changes: 0 additions & 14 deletions scribe/lib/misc_helpers.rb

This file was deleted.

164 changes: 0 additions & 164 deletions scribe/lib/page.rb

This file was deleted.

47 changes: 0 additions & 47 deletions scribe/lib/page_helpers.rb

This file was deleted.

10 changes: 0 additions & 10 deletions scribe/lib/pages.rb

This file was deleted.

1 change: 0 additions & 1 deletion scribe/lib/scribe.rb

This file was deleted.

23 changes: 0 additions & 23 deletions scribe/lib/scribe_extension.rb

This file was deleted.

6 changes: 3 additions & 3 deletions source/javascripts/site.js
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

0 comments on commit ca285ef

Please sign in to comment.