Permalink
Browse files

initial implementation

  • Loading branch information...
1 parent 1a53508 commit 32ae634240bc3af7e5e777f90aa4b12ed588bb18 @phlipper committed Dec 31, 2012
View
@@ -0,0 +1,3 @@
+source "https://rubygems.org"
+
+gemspec
View
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Phil Cohen
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,71 @@
+# forem-kramdown
+
+## Description
+
+Provides [Kramdown](http://kramdown.rubyforge.org/) markup (with syntax highlighting by [CodeRay](http://coderay.rubychan.de/)) for [Forem](https://github.com/radar/forem) posts.
+
+This gem is based on [forem-redcarpet](https://github.com/radar/forem-redcarpet).
+
+
+## Requirements
+
+This gem requires Rails 3.1+ and Forem 1.0.0.beta1.
+
+This gem has been tested against the following Ruby versions:
+
+* 1.8.7
+* 1.9.2
+* 1.9.3
+* JRuby
+* Rubinius
+
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+```ruby
+gem "forem-kramdown"
+```
+
+And then execute:
+
+```
+$ bundle
+```
+
+Or install it yourself as:
+
+```
+$ gem install forem-kramdown
+```
+
+
+## Usage
+
+Specify this gem as a dependency of an application that also contains Forem:
+
+```ruby
+gem "forem", git: "git://github.com/radar/forem"
+gem "forem-kramdown"
+```
+
+The code will do the rest!
+
+
+## Contributing
+
+1. [Fork it](https://github.com/phlipper/forem-kramdown/fork_select)
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. [Create a Pull Request](hhttps://github.com/phlipper/forem-kramdown/pull/new)
+
+
+## License
+
+**sugar-rails**
+
+* Freely distributable and licensed under the [MIT license](http://phlipper.mit-license.org/2012/license.html).
+* Copyright (c) 2012 Phil Cohen (github@phlippers.net) [![endorse](http://api.coderwall.com/phlipper/endorsecount.png)](http://coderwall.com/phlipper)
+* http://phlippers.net/
View
@@ -0,0 +1,12 @@
+require "bundler/gem_tasks"
+
+require "rake/testtask"
+
+task default: :test
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << "lib"
+ t.libs << "test"
+ t.pattern = "test/**/*_test.rb"
+ t.verbose = false
+end
View
@@ -0,0 +1,30 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path("../lib", __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require "forem-kramdown/version"
+
+Gem::Specification.new do |gem|
+ gem.name = "forem-kramdown"
+ gem.version = Forem::Kramdown::VERSION
+ gem.authors = ["Phil Cohen"]
+ gem.email = ["github@phlippers.net"]
+ gem.description = "Provides Kramdown markup (with syntax highlighting by CodeRay) for Forem posts"
+ gem.summary = "Provides Kramdown markup (with syntax highlighting by CodeRay) for Forem posts"
+ gem.homepage = "http://phlippers.net/forem-kramdown"
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+
+ gem.add_dependency "activesupport", ">= 3.1.0"
+ gem.add_dependency "coderay", "~> 1.0.8"
+ gem.add_dependency "kramdown", "~> 0.14.1"
+ gem.add_dependency "nokogiri", "~> 1.5.6"
+
+ gem.add_development_dependency "cane", "~> 2.5.0"
+ gem.add_development_dependency "coveralls", "~> 0.5.5"
+ gem.add_development_dependency "minitest", "~> 4.3.0"
+ gem.add_development_dependency "rake", "~> 10.0.0"
+ gem.add_development_dependency "simplecov", "~> 0.7.1"
+end
View
@@ -0,0 +1,5 @@
+require "forem/formatters/kramdown"
+
+if Forem.respond_to?(:formatter=)
+ Forem.formatter = Forem::Formatters::Kramdown
+end
@@ -0,0 +1,5 @@
+module Forem
+ module Kramdown
+ VERSION = "0.1.0"
+ end
+end
@@ -0,0 +1,32 @@
+require "active_support/core_ext/string"
+require "coderay"
+require "kramdown"
+require "nokogiri"
+
+module Forem
+ module Formatters
+
+ # Public: Various methods useful for formatting text with Kramdown.
+ class Kramdown
+ def self.format(text)
+ renderer = ::Kramdown::Document.new(text.to_s)
+ syntax_highlight(renderer.to_html).html_safe
+ end
+
+ def self.blockquote(text)
+ text.to_s.split("\n").map { |line| "> #{line}" }.join("\n")
+ end
+
+ def self.syntax_highlight(html)
+ doc = ::Nokogiri::HTML::DocumentFragment.parse(html.to_s)
+ doc.css("pre code[@class]").each do |code|
+ line_numbers = { :line_numbers => :inline }
+ language = code[:class].to_sym
+ syntax = CodeRay.scan(code.text.rstrip, language).div(line_numbers)
+ code.replace %(<div class="forem_highlight">#{syntax}</div>)
+ end
+ doc.to_s.html_safe
+ end
+ end
+ end
+end
@@ -0,0 +1,45 @@
+require "minitest_helper"
+
+describe Forem::Formatters::Kramdown do
+ subject { Forem::Formatters::Kramdown }
+
+ it "exists" do
+ subject.new.must_be_kind_of Forem::Formatters::Kramdown
+ end
+
+ describe "self.format" do
+ let(:plain_text) { "this is not markdown" }
+ let(:plain_html) { "<p>this is not markdown</p>" }
+ let(:markdown_text) { "test *is* **markdown**" }
+ let(:markdown_html) { "<p>test <em>is</em> <strong>markdown</strong></p>" }
+
+ it { subject.format(plain_text).must_equal plain_html }
+ it { subject.format(markdown_text).must_equal markdown_html }
+ end
+
+ describe "self.blockquote" do
+ it { subject.blockquote("hello").must_equal "> hello" }
+ it { subject.blockquote("hello\nworld").must_equal "> hello\n> world" }
+ end
+
+ describe "self.syntax_highlight" do
+ let(:ruby_html) { %(<pre><code class="ruby">"hello world"</code></pre>) }
+
+ it { subject.syntax_highlight("hello").must_equal "hello" }
+ it {
+ subject.syntax_highlight(ruby_html).must_match(
+ %(<div class="forem_highlight">)
+ )
+ }
+ it {
+ subject.syntax_highlight(ruby_html).must_match(
+ %(<div class="CodeRay">)
+ )
+ }
+ it {
+ subject.syntax_highlight(ruby_html).must_match(
+ %(<span class="line-numbers"><a href="#n1" name="n1">1</a></span>)
+ )
+ }
+ end
+end
View
@@ -0,0 +1,25 @@
+require "simplecov"
+require "coveralls"
+
+SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
+ SimpleCov::Formatter::HTMLFormatter,
+ Coveralls::SimpleCov::Formatter
+]
+
+SimpleCov.start do
+ add_filter "test"
+ command_name "MiniTest"
+end
+
+require "minitest/autorun"
+require "minitest/pride"
+
+# Note: `Forem` must respond to `.formatter=` before the tests are run for
+# SimpleCov to register the coverage. This avoids the need to bring along
+# Forem and Rails with this library.
+module Forem
+ def self.formatter=(formatter)
+ end
+end
+
+require File.expand_path("../../lib/forem-kramdown", __FILE__)

0 comments on commit 32ae634

Please sign in to comment.