Skip to content

Commit

Permalink
Merge pull request #7 from gjtorikian/more-testing
Browse files Browse the repository at this point in the history
More testing
  • Loading branch information
gjtorikian committed Jun 26, 2015
2 parents 3b2dba7 + 559164d commit b8373b3
Show file tree
Hide file tree
Showing 21 changed files with 3,206 additions and 2,466 deletions.
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source "https://rubygems.org/"
source 'https://rubygems.org/'

gemspec

group :benchmark do
gem "redcarpet"
gem "kramdown"
gem "github-markdown"
gem 'redcarpet'
gem 'kramdown'
gem 'github-markdown'
end
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014 Garen J. Torikian
Copyright (c) 2015 Garen J. Torikian

MIT License

Expand Down
85 changes: 63 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# commonmarker
# CommonMarker

[![Build Status](https://travis-ci.org/gjtorikian/commonmarker.svg)](https://travis-ci.org/gjtorikian/commonmarker) [![Gem Version](https://badge.fury.io/rb/commonmarker.svg)](http://badge.fury.io/rb/commonmarker)

Expand All @@ -21,28 +21,42 @@ Or install it yourself as:

## Usage

### Printing to HTML
### Converting to HTML

Simply put, you'll first need to parse a string to receive a `Document` node. You can than print that node to HTML. For example:
Call `render_html` on a string to convert it to HTML:

``` ruby
require 'commonmarker'
CommonMarker.render_html('Hi *there*', :default)
# <p>Hi <em>there</em></p>\n
```

The second argument is optional--[see below](#options) for more information.

### Generating a document

doc = CommonMarker::Node.parse_string('*Hello* world')
puts(doc.to_html)
You can also parse a string to receive a `Document` node. You can than print that node to HTML, iterate over the children, and other fun node stuff. For example:

``` ruby
require 'commonmarker'

doc = CommonMarker.render_doc('*Hello* world', :default)
puts(doc.to_html) # <p>Hi <em>there</em></p>\n

doc.walk do |node|
puts node.type
puts node.type # [:document, :paragraph, :text, :emph, :text]
end
```

### Walking the AST
The second argument is optional--[see below](#options) for more information.

#### Example: walking the AST

``` ruby
require 'commonmarker'

# parse the files specified on the command line
doc = CommonMarker::Node.parse_string("# The site\n\n [GitHub](https://www.github.com)")
doc = CommonMarker.render_doc("# The site\n\n [GitHub](https://www.github.com)")

# Walk tree and print out URLs for links
doc.walk do |node|
Expand Down Expand Up @@ -76,7 +90,7 @@ end
You can also derive a class from CommonMarker's `HtmlRenderer` class. This produces slower output, but is far more customizable. For example:

``` ruby
class MyHtmlRenderer < HtmlRenderer
class MyHtmlRenderer < CommonMarker::HtmlRenderer
def initialize
super
@headerid = 1
Expand All @@ -97,11 +111,49 @@ print(myrenderer.render(doc))

# Print any warnings to STDERR
renderer.warnings.each do |w|
STDERR.write(w)
STDERR.write("\n")
STDERR.write("#{w}\n")
end
```

## Options

CommonMarker accepts the same options that CMark does, as symbols:

* `:default` - The default rendering.
* `:sourcepos` - Include source position in rendered HTML.
* `:hardbreaks` - Treat `\n` as hardbreaks (by adding `<br/>`).
* `:normalize` - Attempt to normalize the HTML.
* `:smart` - Use smart punctuation (curly quotes, etc.).

Pass these in as a single symbol argument:

``` ruby
require 'commonmarker'
CommonMarker.render_html("\"Hello,\" said the spider.", :smart)
# <p>“Hello,” said the spider.</p>\n
```

To have multiple options applied, pass in an array of options:

``` ruby
require 'commonmarker'
CommonMarker.render_html("\"Hello,\" said the spider.\n\"'Shelob' is my name.\"", [:hardbreaks, :smart])
# <p>“Hello,” said the spider.</br>“‘Shelob’ is my name.”</p>
```

For more information on these options, see [the CMark documentation](http://git.io/vLIHE).

## Hacking

After cloning the repo:

```
script/bootstrap
bundle exec rake compile
```

If there were no errors, you're done! Otherwise, make sure to follow the CMark dependency instructions.

## Benchmarks

Some rough benchmarks:
Expand All @@ -123,14 +175,3 @@ kramdown
4.610000 0.070000 4.680000 ( 4.678398)
```

## Hacking

After cloning the repo:

```
script/bootstrap
bundle exec rake compile
```

If there were no errors, you're done! Otherwise, make sure to follow the CMark dependency instructions.
3 changes: 2 additions & 1 deletion commonmarker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'commonmarker/version'

Gem::Specification.new do |s|
s.name = 'commonmarker'
s.version = CommonMarker::VERSION
s.summary = 'CommonMark parser and renderer. Written in C, wrapped in Ruby.'
s.description = 'A fast, safe, extensible parser for CommonMark.'
s.description = 'A fast, safe, extensible parser for CommonMark. This wraps the official libcmark library.'
s.authors = ['Garen Torikian']
s.email = ['gjtorikian@gmail.com']
s.homepage = 'http://github.com/gjtorikian/commonmarker'
Expand Down
2 changes: 1 addition & 1 deletion ext/commonmarker/cmark
Submodule cmark updated from 0e4769 to 2f9450
Loading

0 comments on commit b8373b3

Please sign in to comment.