Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renamed PageSet check_mods to check_fs_mods. Reworked Page#commit, re…

…moved a nasty infinite loop. Misc Tweaks.
  • Loading branch information...
commit 793138bfd25395ab9e1da5e0b66bfd04a1157d0d 1 parent e4cf65c
@cowboy authored
View
18 lib/gaucho/commit.rb
@@ -43,18 +43,14 @@ def url
# If a commit_id wasn't passed, this commit is simulated. This should only
# be used when a new Page, with no commits, is being previewd from the
- # filesystem, using check_mods.
+ # filesystem, using check_fs_mods.
def simulated?
@commit_id.nil?
end
# Is this commit the most recent actual (not simulated) commit for the Page?
def latest?
- if page.has_local_mods?
- self == page.commits[-2]
- else
- self == page.commits.last
- end
+ self == page.latest_actual_commit
end
# Is this commit the currently shown commit for the Page?
@@ -93,9 +89,11 @@ def author
# The date of this Commit. A specified metadata "date" will be used first,
# with a fallback to the actual Grit::Commit committed_date.
def date
- Time.parse(meta.date)
- rescue
- commit.committed_date
+ if meta.date.nil? || meta.date.empty?
+ commit.committed_date
+ else
+ Time.parse(meta.date)
+ end
end
# The underlying Grit::Commit instance for this Commit. If this commit is
@@ -104,7 +102,7 @@ def commit
@commit ||= if simulated?
sha = 'f' * 40
actor = Grit::Actor.from_string('John Q. Author')
- time = page.date
+ time = page.files_last_modified
Grit::Commit.new(pageset.repo, sha, [sha], sha, actor, time, actor, time,
%w{This commit is simulated!})
else
View
66 lib/gaucho/page.rb
@@ -3,20 +3,13 @@ class Page
include ShortSha
include StringUtils
- attr_reader :pageset, :id, :path, :commits, :commit, :shown
+ attr_reader :pageset, :id, :path, :commit, :shown, :files_last_modified
def initialize(pageset, id, path, commit_ids)
@pageset = pageset
@id = id
@path = path
@commit_ids = commit_ids
-
- @commits = commit_ids.collect {|commit_id| Gaucho::Commit.new(self, commit_id)}
-
- if !committed? || has_local_mods?
- @commits.push(Gaucho::Commit.new(self))
- end
-
self.shown = nil
rescue
raise Gaucho::PageNotFound
@@ -40,7 +33,7 @@ def shown=(commit_id)
@commit = nil
if commit_id.nil?
- @meta = @files = nil if pageset.check_mods
+ @meta = @files = @commits = nil if check_fs_mods?
else
@commit = commits.find {|commit| commit.id.start_with? commit_id}
end
@@ -48,6 +41,23 @@ def shown=(commit_id)
@commit ||= commits.last
end
+ # Get all Commits for this Page. If the Page hasn't yet been committed or
+ # it has local modifications, append a simulated Commit.
+ def commits
+ unless @commits
+ @commits = @commit_ids.collect {|commit_id| Gaucho::Commit.new(self, commit_id)}
+ if has_fs_mods?
+ @commits << Gaucho::Commit.new(self)
+ end
+ end
+ @commits
+ end
+
+ # The most recent actual (not simulated) commit for this Page.
+ def latest_actual_commit
+ commits.reverse.find {|commit| !commit.simulated?}
+ end
+
# Returns true if this Page's id matches the passed date. If no date is
# passed, returns true if this Page's id begins with a date.
def date?(date_arr = nil)
@@ -60,9 +70,9 @@ def date?(date_arr = nil)
end
# Metadata for the Page at the currently "shown" Commit, or from the index
- # file in the filesystem if shown_local_mods? is true.
+ # file in the filesystem if shown_fs_mods? is true.
def meta
- if shown_local_mods?
+ if shown_fs_mods?
unless @meta
index = Gaucho::Config.new
index.name = Dir.entries(abs_page_path).find {|file| file =~ /^index\./}
@@ -76,9 +86,9 @@ def meta
end
# File listing for the Page at the currently "shown" Commit, or from the
- # filesystem if shown_local_mods? is true.
+ # filesystem if shown_fs_mods? is true.
def files
- if shown_local_mods?
+ if shown_fs_mods?
@files
else
commit.files
@@ -91,10 +101,10 @@ def /(file)
end
# Either the last commit's committed date, or the most recent file last
- # modified time, if shown_local_mods? is true.
+ # modified time, if shown_fs_mods? is true.
def date
- if shown_local_mods?
- @files_last_modified
+ if shown_fs_mods?
+ files_last_modified
else
commits.last.date
end
@@ -119,25 +129,29 @@ def committed?
!@commit_ids.empty?
end
- # If the PageSet "check_mods" option is set and the shown commit is nil,
- # check to see if the local filesystem has modificiations by building a
- # filesystem-based file index and comparing it with the file index of the
- # last Commit.
- def has_local_mods?
- if pageset.check_mods
+ # Is the PageSet "check_fs_mods" option set?
+ def check_fs_mods?
+ pageset.check_fs_mods
+ end
+
+ # If check_fs_mods? is true and the shown commit is nil, check to see if the
+ # local filesystem has modificiations by building a filesystem-based file
+ # index and comparing it with the file index of the last Commit.
+ def has_fs_mods?
+ if check_fs_mods?
build_file_index!
- @files != commits.last.files
+ !committed? || @files != latest_actual_commit.files
end
end
# Are local modifications currently being shown?
- def shown_local_mods?
- shown.nil? && has_local_mods?
+ def shown_fs_mods?
+ shown.nil? && has_fs_mods?
end
# Sort pages by last commit date (most recent first) by default.
def <=>(other)
- other.commits.last.committed_date <=> commits.last.committed_date
+ other.date <=> date
end
# Pass-through all other methods to the underlying metadata object.
View
10 lib/gaucho/pageset.rb
@@ -6,7 +6,7 @@ class PageSet
extend Forwardable
attr_reader :repo, :tree, :subdir, :renames
- attr_accessor :check_mods
+ attr_accessor :check_fs_mods
# Forward Array methods to @pages (via the pages method) so that the PageSet
# can feel as Array-like as possible.
@@ -20,7 +20,7 @@ def initialize(repo, options = {})
end
# Initialize from options, overriding these defaults.
- { check_mods: false,
+ { check_fs_mods: false,
renames: {},
subdir: nil
}.merge(options).each do |key, value|
@@ -32,7 +32,7 @@ def initialize(repo, options = {})
# Rebuild all internal commit / Page caches. Use this to force Gaucho to
# show changes if the repo's commits / HEAD have been changed, or if the
- # check_mods option has been changed.
+ # check_fs_mods option has been changed.
def rebuild!
@tree = if subdir.nil?
repo.tree
@@ -132,12 +132,12 @@ def build_commit_index!
end
# Generate a map of renamed Page id (path) to original Page id. If the
- # check_mods option is enabled, get the listing from the filesystem,
+ # check_fs_mods option is enabled, get the listing from the filesystem,
# otherwise build the map from git.
def build_page_map!
@page_paths = {}
- if check_mods
+ if check_fs_mods
Dir.entries(abs_subdir_path).each do |file|
path = "#{abs_subdir_path}/#{file}"
if FileTest.directory?(path) && !File.basename(path).start_with?('.')
View
25 sample_app/app.rb
@@ -55,7 +55,7 @@ class App < Sinatra::Base
set :root, File.dirname(__FILE__)
set :haml, format: :html5, attr_wrapper: '"'
- check_mods = development?
+ check_fs_mods = development?
renames = {
'paean-article' => 'paean-article-new-url',
'invidious-article' => 'invidious-article-new-url',
@@ -63,12 +63,20 @@ class App < Sinatra::Base
'piste-article' => 'piste-article-new-url',
}
#$pageset = Gaucho::PageSet.new('../spec/test_repo/bare.git', renames: renames)
- $pageset = Gaucho::PageSet.new('../spec/test_repo/small', check_mods: check_mods, renames: renames)
- #$pageset = Gaucho::PageSet.new('../spec/test_repo/huge', check_mods: check_mods, renames: renames)
- #$pageset = Gaucho::PageSet.new('../spec/test_repo/double', check_mods: check_mods, renames: renames, subdir: 'yay')
- #$pageset = Gaucho::PageSet.new('../spec/test_repo/double', check_mods: check_mods, renames: renames, subdir: 'nay')
+ $pageset = Gaucho::PageSet.new('../spec/test_repo/small', check_fs_mods: check_fs_mods, renames: renames)
+ #$pageset = Gaucho::PageSet.new('../spec/test_repo/huge', check_fs_mods: check_fs_mods, renames: renames)
+ #$pageset = Gaucho::PageSet.new('../spec/test_repo/double', check_fs_mods: check_fs_mods, renames: renames, subdir: 'yay')
+ #$pageset = Gaucho::PageSet.new('../spec/test_repo/double', check_fs_mods: check_fs_mods, renames: renames, subdir: 'nay')
+
+ #$pageset = Gaucho::PageSet.new('/Users/cowboy/Sites/benalman.com/new/ba-import/new', check_fs_mods: check_fs_mods)
=begin
+$pageset.pages.each do |page|
+ if page.meta.tags.nil? || page.meta.tags.empty?
+ puts page.id
+ p page.meta.tags
+ end
+end
#ap $pageset
p Renderer.filter_from_name('foo.txt')
p Renderer.filter_from_name('foo.text')
@@ -78,7 +86,6 @@ class App < Sinatra::Base
p Renderer.filter_from_name('foo.html')
p Renderer.filter_from_name('foo.bar')
pg = $pageset['unicode-article']
- pg.check_local_mods
p pg.title
p '== files =='
pg.files.each do |name, data|
@@ -220,11 +227,11 @@ def render_diff(diff)
@page = $pageset[name]
raise Sinatra::NotFound if @page.nil?
if @page.class == String
- redirect @page, 301
+ redirect @page, 302 # 301
# cache?
- return
+ return #"redirect to #{@page}"
end
-
+
@page.shown = sha
if sha && production?
View
2  sample_app/views/index.haml
@@ -5,7 +5,7 @@ Recent content:
- @pages.each do |p|
%li
%a{ href: p.url }= p.title
- - if p.has_local_mods?
+ - if p.has_fs_mods?
%i
%b MODIFIED
%div= p.subtitle
View
12 sample_app/views/page.haml
@@ -1,16 +1,18 @@
%h1= @title
+- if @page.subtitle
+ %p.subtitle= @page.subtitle
-%p Last updated on #{date_format(@page.date)}
+%p.last_updated Last updated on #{date_format(@page.date)}
-- if @page.has_local_mods?
+- if @page.has_fs_mods?
%p
%em
Note:
- - if @page.shown_local_mods?
+ - if @page.shown_fs_mods?
Showing local modifications.
- if @page.committed?
View the
- %a{ href: @commits[-2].url } most recent commit
+ %a{ href: @page.latest_actual_commit.url } most recent commit
instead.
- else
This page hasn't been committed yet.
@@ -19,7 +21,7 @@
= succeed '.' do
%a{ href: @page.url } local modifications
-- elsif @commit.latest?
+- elsif !@commit.latest?
%p
%em
The content you are viewing is out of date. View the
View
27 spec/create_test_repo.rb
@@ -34,7 +34,7 @@ def add_and_rotate(arr, n = nil)
end
class TestRepoBuilder
- attr_accessor :titles, :titles_check_mods, :alt_titles, :page_subdirs
+ attr_accessor :titles, :titles_check_fs_mods, :alt_titles, :page_subdirs
def initialize(path)
print %Q{Building repo "#{path}"...}
@@ -51,7 +51,7 @@ def initialize(path)
def init_ivars
@titles = []
- @titles_check_mods = []
+ @titles_check_fs_mods = []
@alt_titles = []
@all_cats = %w(news projects articles)
@@ -193,6 +193,19 @@ def create_article(title)
[{{ #{incl} }} Super Cowboy Hats!! {{ #{incl} }}]({{ #{incl} | url }})
+<!--
+TODO: UNIT TEST!
+{{ #{incl} | image }}
+{{ #{incl} | image(This is a test 1!) }}
+{{ #{incl} | image(This is a "test" with (parens), lamen't?) }}
+{{ #{incl} | image(alt: "This is a test 2!") }}
+{{ #{incl} | image(:alt => "This is a test 4!") }}
+{{ #{incl} | image("alt" => "This is a test 5!") }}
+{{ #{incl} | image(alt: "This is a test 6!", width: 100) }}
+{{ #{incl} | image(:alt => "This is a test 7!", :width => 100) }}
+{{ #{incl} | image("alt" => "This is a test 8!", "width" => 100) }}
+-->
+
#{@all_more_toc.shift_rotate(1)[0]}
EOF
docs
@@ -324,7 +337,7 @@ def do_stuff
`git checkout -q master`
# modify a few articles (uncommitted)
- @titles_check_mods.each do |title|
+ @titles_check_fs_mods.each do |title|
docs = read_index(title)
docs[0]['Title'] += '!!!'
docs[1].gsub!(/(This is a sample article)/, '\1 with **LOCAL MODIFICATIONS**')
@@ -374,14 +387,14 @@ def do_stuff
TestRepoBuilder.new('small') do |trb|
trb.titles = @titles.shift(10)
- trb.titles_check_mods = trb.titles[0..2]
+ trb.titles_check_fs_mods = trb.titles[0..2]
trb.alt_titles = @alt_titles.shift(1)
trb.do_stuff
end
-TestRepoBuilder.new('double') do |trb|
+false && TestRepoBuilder.new('double') do |trb|
trb.titles = @titles.shift(20)
- trb.titles_check_mods = trb.titles[0..4]
+ trb.titles_check_fs_mods = trb.titles[0..4]
trb.alt_titles = @alt_titles.shift(2)
trb.page_subdirs = ['yay/', 'nay/']
trb.do_stuff
@@ -389,7 +402,7 @@ def do_stuff
false && TestRepoBuilder.new('huge') do |trb|
trb.titles = @titles.shift(100)
- trb.titles_check_mods = trb.titles[0..10]
+ trb.titles_check_fs_mods = trb.titles[0..10]
trb.alt_titles = @alt_titles.shift(4)
trb.page_subdirs = []
trb.do_stuff
Please sign in to comment.
Something went wrong with that request. Please try again.