Skip to content

Commit

Permalink
syntax highlighting via the syntax gem
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wiggins committed Oct 24, 2008
1 parent 7d76447 commit a09dd2d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lib/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
end

require 'rdiscount'
require 'syntax/convertors/html'

class Post < Sequel::Model
def url
Expand Down Expand Up @@ -44,7 +45,11 @@ def linked_tags
########

def to_html(markdown)
RDiscount.new(markdown).to_html.gsub(/\<code\>\n/, '<code>')
h = RDiscount.new(markdown).to_html
h.gsub(/<code>([^<]+)<\/code>/m) do |block|
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
"<code>#{convertor.convert($1)}</code>"
end
end

def split_content(string)
Expand Down
35 changes: 29 additions & 6 deletions public/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,6 @@ hr {
color: #525252;
}

code {
font-family: Monaco, mono;
color: #ccc;
white-space: pre;
}

/* Archive */
.archive .date {
font-size: 85%;
Expand All @@ -241,3 +235,32 @@ code {
color: #FFFFDC;
}

/* Code blocks */

pre {
font-family: Monaco, mono;
color: #ccc;
background: #000;
border-left: 6px solid #222;
padding: 0.4em;
padding-left: 0.8em;
}

code span.attribute { color: #009900; }
code span.char { color: #F00; }
code span.class { color: #A020F0; font-weight: bold; }
code span.comment { color: #0000FF; }
code span.constant { color: #008B8B; }
code span.escape { color: #6A5ACD; }
code span.expr { color: #2222CC; }
code span.global { color: #11AA44; }
code span.ident { color: #CCCCCC; }
code span.keyword { color: #A52A2A; font-weight: bold; }
code span.method { color: #008B8B; }
code span.module { color: #A020F0; font-weight: bold; }
code span.number { color: #DD00DD; }
code span.punct { color: #6A5ACD; }
code span.regex { color: #DD00DD; }
code span.string { color: #DD00DD; }
code span.symbol { color: #008B8B; }

6 changes: 3 additions & 3 deletions spec/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
end

it "has a url in simplelog format: /past/2008/10/17/my_post/" do
@post.created_at = Time.now
@post.created_at = '2008-10-22'
@post.slug = "my_post"
@post.url.should == '/past/2008/10/22/my_post/'
end
Expand All @@ -16,8 +16,8 @@
@post.body_html.should == "<ul>\n<li>Bullet</li>\n</ul>\n\n"
end

it "gets rid of the newline after the code tag, since code is set to whitespace: pre" do
@post.to_html("<code>\none\ntwo</code>").should == "<p><code>one\ntwo</code></p>\n"
it "syntax highlights code blocks" do
@post.to_html("<code>\none\ntwo</code>").should == "<p><code><pre>\n<span class=\"ident\">one</span>\n<span class=\"ident\">two</span></pre></code></p>\n"
end

it "makes the tags into links to the tag search" do
Expand Down

0 comments on commit a09dd2d

Please sign in to comment.