Skip to content

Commit

Permalink
Add Textile support. Requires RedCloth gem.
Browse files Browse the repository at this point in the history
  • Loading branch information
tuupola authored and gma committed Feb 26, 2010
1 parent d581277 commit d86ab15
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
10 changes: 9 additions & 1 deletion lib/models.rb
Expand Up @@ -2,9 +2,10 @@

require "rubygems"
require "maruku"
require "redcloth"

class FileModel
FORMATS = [:mdown, :haml]
FORMATS = [:mdown, :haml, :textile]
@@cache = {}

attr_reader :filename, :mtime
Expand Down Expand Up @@ -66,6 +67,8 @@ def to_html
Maruku.new(markup).to_html
when :haml
Haml::Engine.new(markup).to_html
when :textile
RedCloth.new(markup).to_html
end
end

Expand Down Expand Up @@ -147,6 +150,8 @@ def heading
/^#\s*(.*)/
when :haml
/^\s*%h1\s+(.*)/
when :textile
/^\s*h1\.\s+(.*)/
end
markup =~ regex
Regexp.last_match(1)
Expand Down Expand Up @@ -182,6 +187,9 @@ def body
when :haml
body_text = markup.sub(/^\s*%h1\s+.*$\r?\n(\r?\n)?/, "")
Haml::Engine.new(body_text).render
when :textile
body_text = markup.sub(/^\s*h1\.\s+.*$\r?\n(\r?\n)?/, "")
RedCloth.new(body_text).to_html
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/app_spec.rb
Expand Up @@ -292,7 +292,7 @@
end

describe "that is configured to show Disqus comments" do
setup do
before(:each) do
stub_config_key("disqus_short_name", "mysite")
@category = create_category
get @category.abspath
Expand Down
9 changes: 7 additions & 2 deletions spec/model_factory.rb
Expand Up @@ -87,14 +87,19 @@ def create_file(path, options = {})
create_content_directories
metadata = options[:metadata] || {}
metatext = metadata.map { |key, value| "#{key}: #{value}" }.join("\n")
prefix = (options[:ext] == :haml) ? "%div\n %h1" : '# '
if options[:ext] == :haml
prefix = "%div\n %h1"
elsif options[:ext] == :textile
prefix = "<div>\nh1."
else
prefix = '# '
end
heading = options[:heading] ? "#{prefix} #{options[:heading]}\n\n" : ""
contents =<<-EOF
#{metatext}
#{heading}#{options[:content]}
EOF

FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") { |file| file.write(contents) }
end
Expand Down
13 changes: 13 additions & 0 deletions spec/models_spec.rb
Expand Up @@ -297,3 +297,16 @@ def create_page(options)

it_should_behave_like "Page"
end

describe "Textile page" do
before(:each) do
@extension = :textile
end

it "should set heading from first h1 tag" do
create_article(:path => "headings", :content => 'h1. Second heading')
Page.find_by_path("headings").heading.should == "My article"
end

it_should_behave_like "Page"
end

0 comments on commit d86ab15

Please sign in to comment.