Skip to content

Commit

Permalink
Source links.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Jul 27, 2011
1 parent 5efee93 commit 34f2c57
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Scribefile
Expand Up @@ -4,3 +4,5 @@ files:
- source: lib/**/*.rb

output: doc

github: rstacruz/proscribe
5 changes: 0 additions & 5 deletions data/default/Protonfile
Expand Up @@ -7,8 +7,3 @@ tilt_options:
haml:
ugly: true

# Custom stuff
extractor:
files:
- source: ../lib/**/*.rb
target: api/
9 changes: 9 additions & 0 deletions data/default/_extensions/helpers.rb
Expand Up @@ -10,6 +10,15 @@ def page_children(page)
type.nil? ? nil : Inflector[type].pluralize.to_sym
}
end

# A link to the source file on GitHub
def github_source_link
if project.config.github && project.config.git
if page.meta.source_file
"https://github.com/#{project.config.github}/blob/#{project.config.git[0..6]}/#{page.source_file}#L#{page.source_line}".squeeze('/')
end
end
end
end

# Inflector['hello'].pluralize
Expand Down
22 changes: 22 additions & 0 deletions data/default/_layouts/default.haml
Expand Up @@ -57,6 +57,28 @@
- unless method.meta.brief.to_s.empty?
%span.brief= method.meta.brief

-# Source
%section.footer
.left
%strong
- page.breadcrumbs[1...-1].each do |pp|
%a{href: rel(page.path)}
= pp.title
-# if pp.meta.page_type
-# %span.type= pp.meta.page_type
%span.arrow!= "›"

= page.title
- if page.meta.page_type
%span.type= page.meta.page_type

- if github_source_link
Defined in
%a{href: github_source_link}
%span= page.source_file
%span.view-source!= "View source ›"

%nav#nav
- parent = (page.children.any? ? page : (page.parent || page))
- children = parent.children.select { |p| p.html? }
Expand Down
51 changes: 51 additions & 0 deletions data/default/style.scss
Expand Up @@ -135,6 +135,7 @@ $pad: 40px;
#content {
@include border-top-radius(2px);
background: white; padding: 40px;
padding-bottom: 140px;
position: relative;
@include box-sizing(border-box);
@include clearfix;
Expand Down Expand Up @@ -441,6 +442,56 @@ section.aux-links {
text-shadow: 1px 1px 0 rgba(black, 0.1); }
}

section.footer {
background: #eee;
border-top: solid 1px #eee;

position: absolute;
@include box-sizing(border-box);
bottom: 0; left: 0; width: 100%;
padding: 20px;


font-size: 0.75em;

color: #aaa;
text-shadow: 1px 1px 0 rgba(white, 0.5);

@include box-shadow(inset 0 3px 3px rgba(black, 0.1));
text-align: left;

a, strong {
color: #888; }

strong {
font-size: 1.1em; }

span.type {
opacity: 0.7;
font-weight: normal; font-size: 0.9em;
&::before { content: '['; }
&::after { content: ']'; } }

span.arrow {
margin: 0 5px; }

.left {
strong { display: block; } }

.line {
font-style: normal; }

.view-source {
background: rgba(black, 0.1);
color: #888;
padding: 1px 8px;
font-size: 0.9em;
font-weight: normal;
margin-left: 5px;
@include border-radius(7px);
font-style: normal; }
}

//
// Prettify
//
Expand Down
32 changes: 23 additions & 9 deletions lib/proscribe/extractor.rb
Expand Up @@ -17,8 +17,9 @@ module ProScribe
# ex.write!('manual/') # Writes to manual/
#
class Extractor
def initialize(files, options={})
def initialize(files, root, options={})
@files = files
@root = File.realpath(root)
end

def write!(output_path = '.', &blk)
Expand All @@ -36,50 +37,60 @@ def blocks
@files.map { |file|
if File.file?(file)
input = File.read(file)
get_blocks input
get_blocks(input, unroot(file))
end
}.compact.flatten
end
end

private
def unroot(fn)
(File.realpath(fn))[@root.size..-1]
end

# Returns blocks that match a blah.
def get_blocks(str)
def get_blocks(str, filename)
arr = get_comment_blocks(str)
arr.map { |block|
arr.map { |hash|
block = hash[:block]
re = /^([A-Za-z ]*?): (.*?)(?: \((.*?)\))?$/

if block.last =~ re
Extractor::Block.new \
:type => $1,
:title => $2,
:parent => $3,
:line => hash[:line] + block.size + 1,
:source => filename,
:body => (block[0..-2].join("\n") + "\n")
elsif block.first =~ re
Extractor::Block.new \
:type => $1,
:title => $2,
:parent => $3,
:line => hash[:line] + block.size + 1,
:source => filename,
:body => (block[1..-1].join("\n") + "\n")
end
}.compact
end

# Returns contiguous comment blocks.
# Returns an array of hashes (:block => [line1,line2...], :line => n)
def get_comment_blocks(str)
chunks = Array.new
i = 0

str.split("\n").each { |s|
str.split("\n").each_with_index { |s, line|
if s =~ /^\s*(?:\/\/\/?|##?) ?(.*)$/
chunks[i] ||= Array.new
chunks[i] << $1
chunks[i] ||= { :block => Array.new, :line => line }
chunks[i][:block] << $1
else
i += 1 if chunks[i]
end
}

chunks
chunks.compact
end
end

Expand All @@ -93,11 +104,14 @@ def initialize(options)
body = options[:body]
type = options[:type].downcase

source = options[:source]
line = options[:line]

file = to_filename(title, parent)
brief, *body = body.split("\n\n")
body = "#{body.join("\n\n")}"

heading = "title: #{title}\npage_type: #{type}\nbrief: #{brief}\n"
heading = "title: #{title}\npage_type: #{type}\nsource_file: #{source}\nsource_line: #{line}\nbrief: #{brief}\n"
heading += "--\n"

@file = file
Expand Down
22 changes: 21 additions & 1 deletion lib/proscribe/project.rb
Expand Up @@ -52,13 +52,33 @@ def make
# Copy manual files over
copy_files manual_path, dir, :except => ['Gemfile', 'Gemfile.lock', 'config.ru']

# Merge Scribefile into Protonfile
File.open(File.join(dir, 'Protonfile'), 'w') { |f| f.write protonfile }

# Extract block comments
config.files.each do |group|
ex = ProScribe::Extractor.new Dir[root(group.source)]
ex = ProScribe::Extractor.new(Dir[root(group.source)], root)
ex.write! File.join(dir, group.prefix || '')
end
end

def protonfile
yaml = YAML::load_file(ProScribe.root('data/default/Protonfile'))

# Copy from Scribefile
c = config.to_hash.dup
c.delete 'manual'
c.delete 'output'
c.delete 'files'

# Add some things
c['git'] = `git rev-parse HEAD`.strip

yaml.merge! c

YAML::dump yaml
end

# Attribute: dir (ProScribe::Project)
# Returns the path to the temporary Proton project.
#
Expand Down

0 comments on commit 34f2c57

Please sign in to comment.