Skip to content

Commit bd13b02

Browse files
authored
Merge pull request #185 from gjtorikian/lets-rust
Migrate to comrak/Rust
2 parents e4bbc67 + be3071b commit bd13b02

File tree

122 files changed

+11046
-34612
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+11046
-34612
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
types: [opened, synchronize, reopened]
66
push:
77
branches:
8-
- production
8+
- main
99

1010
jobs:
1111
test:
@@ -16,7 +16,7 @@ jobs:
1616
- ubuntu-latest
1717
- macos-latest
1818
- windows-latest
19-
ruby-version: [3.1, 3.0.0, 2.7.2, 2.6.6]
19+
ruby-version: [3.1, 3.0]
2020

2121
runs-on: ${{ matrix.os }}
2222

@@ -31,6 +31,11 @@ jobs:
3131
ruby-version: ${{ matrix.ruby-version }}
3232
bundler-cache: true # 'bundle install' and cache
3333

34+
- name: Set Cargo triple
35+
if: runner.os == 'Windows'
36+
shell: bash
37+
run: rustup default stable-x86_64-pc-windows-gnu
38+
3439
- name: Run ${{ matrix.os }} tests
3540
shell: bash
3641
run: script/cibuild

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
/InstalledFiles
99
/pkg/
1010
/spec/reports/
11-
/test/tmp/
1211
/test/version_tmp/
13-
/tmp/
12+
tmp/
1413
/vendor/gems
1514
/vendor/cache
1615
Gemfile.lock
16+
*.log
17+
ports/
1718

1819
## Specific to RubyMotion:
1920
.dat*

.gitmodules

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
[submodule "ext/commonmarker/cmark-upstream"]
2-
path = ext/commonmarker/cmark-upstream
3-
url = https://github.com/github/cmark-gfm.git
4-
ignore = dirty

.rubocop.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ inherit_gem:
44
- config/minitest.yml
55

66
AllCops:
7-
TargetRubyVersion: 2.6
87
Exclude:
98
- "ext/**/*"
109
- "vendor/**/*"

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ gemspec
77
group :benchmark do
88
gem "benchmark-ips"
99
gem "kramdown"
10+
gem "kramdown-parser-gfm"
1011
gem "redcarpet"
1112
end

Makefile

Lines changed: 0 additions & 26 deletions
This file was deleted.

README.md

Lines changed: 25 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -29,98 +29,12 @@ Call `render_html` on a string to convert it to HTML:
2929

3030
``` ruby
3131
require 'commonmarker'
32-
CommonMarker.render_html('Hi *there*', :DEFAULT)
32+
CommonMarker.to_html('Hi *there*', :DEFAULT)
3333
# <p>Hi <em>there</em></p>\n
3434
```
3535

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

38-
### Generating a document
39-
40-
You can also parse a string to receive a `Document` node. You can then print that node to HTML, iterate over the children, and other fun node stuff. For example:
41-
42-
``` ruby
43-
require 'commonmarker'
44-
45-
doc = CommonMarker.render_doc('*Hello* world', :DEFAULT)
46-
puts(doc.to_html) # <p>Hi <em>there</em></p>\n
47-
48-
doc.walk do |node|
49-
puts node.type # [:document, :paragraph, :text, :emph, :text]
50-
end
51-
```
52-
53-
The second argument is optional--[see below](#options) for more information.
54-
55-
#### Example: walking the AST
56-
57-
You can use `walk` or `each` to iterate over nodes:
58-
59-
- `walk` will iterate on a node and recursively iterate on a node's children.
60-
- `each` will iterate on a node and its children, but no further.
61-
62-
``` ruby
63-
require 'commonmarker'
64-
65-
# parse the files specified on the command line
66-
doc = CommonMarker.render_doc("# The site\n\n [GitHub](https://www.github.com)")
67-
68-
# Walk tree and print out URLs for links
69-
doc.walk do |node|
70-
if node.type == :link
71-
printf("URL = %s\n", node.url)
72-
end
73-
end
74-
75-
# Capitalize all regular text in headers
76-
doc.walk do |node|
77-
if node.type == :header
78-
node.each do |subnode|
79-
if subnode.type == :text
80-
subnode.string_content = subnode.string_content.upcase
81-
end
82-
end
83-
end
84-
end
85-
86-
# Transform links to regular text
87-
doc.walk do |node|
88-
if node.type == :link
89-
node.insert_before(node.first_child)
90-
node.delete
91-
end
92-
end
93-
```
94-
95-
### Creating a custom renderer
96-
97-
You can also derive a class from CommonMarker's `HtmlRenderer` class. This produces slower output, but is far more customizable. For example:
98-
99-
``` ruby
100-
class MyHtmlRenderer < CommonMarker::HtmlRenderer
101-
def initialize
102-
super
103-
@headerid = 1
104-
end
105-
106-
def header(node)
107-
block do
108-
out("<h", node.header_level, " id=\"", @headerid, "\">",
109-
:children, "</h", node.header_level, ">")
110-
@headerid += 1
111-
end
112-
end
113-
end
114-
115-
myrenderer = MyHtmlRenderer.new
116-
puts myrenderer.render(doc)
117-
118-
# Print any warnings to STDERR
119-
renderer.warnings.each do |w|
120-
STDERR.write("#{w}\n")
121-
end
122-
```
123-
12438
## Options
12539

12640
CommonMarker accepts the same options that CMark does, as symbols. Note that there is a distinction in CMark for "parse" options and "render" options, which are represented in the tables below.
@@ -158,101 +72,28 @@ CommonMarker accepts the same options that CMark does, as symbols. Note that the
15872

15973
### Passing options
16074

161-
To apply a single option, pass it in as a symbol argument:
75+
To apply an option, pass it as part of the hash:
16276

16377
``` ruby
164-
CommonMarker.render_doc("\"Hello,\" said the spider.", :SMART)
78+
CommonMarker.to_html("\"Hello,\" said the spider.", :SMART)
16579
# <p>“Hello,” said the spider.</p>\n
166-
```
16780

168-
To have multiple options applied, pass in an array of symbols:
169-
170-
``` ruby
171-
CommonMarker.render_html("\"'Shelob' is my name.\"", [:HARDBREAKS, :SOURCEPOS])
81+
CommonMarker.to_html("\"'Shelob' is my name.\"", [:HARDBREAKS, :SOURCEPOS])
17282
```
17383

174-
For more information on these options, see [the CMark documentation](https://git.io/v7nh1).
175-
176-
## Extensions
177-
178-
Both `render_html` and `render_doc` take an optional third argument defining the extensions you want enabled as your CommonMark document is being processed. The documentation for these extensions are [defined in this spec](https://github.github.com/gfm/), and the rationale is provided [in this blog post](https://githubengineering.com/a-formal-spec-for-github-markdown/).
179-
180-
The available extensions are:
181-
182-
* `:table` - This provides support for tables.
183-
* `:tasklist` - This provides support for task list items.
184-
* `:strikethrough` - This provides support for strikethroughs.
185-
* `:autolink` - This provides support for automatically converting URLs to anchor tags.
186-
* `:tagfilter` - This escapes [several "unsafe" HTML tags](https://github.github.com/gfm/#disallowed-raw-html-extension-), causing them to not have any effect.
84+
For more information on these options, see [the comrak documentation](https://github.com/kivikakk/comrak#usage).
18785

18886
## Output formats
18987

190-
Like CMark, CommonMarker can generate output in several formats: HTML, XML, plaintext, and commonmark are currently supported.
88+
Commonmarker can only generate output in one format: HTML.
19189

19290
### HTML
19391

194-
The default output format, HTML, will be generated when calling `to_html` or using `--to=html` on the command line.
195-
196-
```ruby
197-
doc = CommonMarker.render_doc('*Hello* world!', :DEFAULT)
198-
puts(doc.to_html)
199-
200-
<p><em>Hello</em> world!</p>
201-
```
202-
203-
### XML
204-
205-
XML will be generated when calling `to_xml` or using `--to=xml` on the command line.
206-
207-
```ruby
208-
doc = CommonMarker.render_doc('*Hello* world!', :DEFAULT)
209-
puts(doc.to_xml)
210-
211-
<?xml version="1.0" encoding="UTF-8"?>
212-
<!DOCTYPE document SYSTEM "CommonMark.dtd">
213-
<document xmlns="http://commonmark.org/xml/1.0">
214-
<paragraph>
215-
<emph>
216-
<text xml:space="preserve">Hello</text>
217-
</emph>
218-
<text xml:space="preserve"> world!</text>
219-
</paragraph>
220-
</document>
221-
```
222-
223-
### Plaintext
224-
225-
Plaintext will be generated when calling `to_plaintext` or using `--to=plaintext` on the command line.
226-
22792
```ruby
228-
doc = CommonMarker.render_doc('*Hello* world!', :DEFAULT)
229-
puts(doc.to_plaintext)
93+
html = CommonMarker.to_html('*Hello* world!', :DEFAULT)
94+
puts(html)
23095

231-
Hello world!
232-
```
233-
234-
### Commonmark
235-
236-
Commonmark will be generated when calling `to_commonmark` or using `--to=commonmark` on the command line.
237-
238-
``` ruby
239-
text = <<-TEXT
240-
1. I am a numeric list.
241-
2. I continue the list.
242-
* Suddenly, an unordered list!
243-
* What fun!
244-
TEXT
245-
246-
doc = CommonMarker.render_doc(text, :DEFAULT)
247-
puts(doc.to_commonmark)
248-
249-
1. I am a numeric list.
250-
2. I continue the list.
251-
252-
<!-- end list -->
253-
254-
- Suddenly, an unordered list\!
255-
- What fun\!
96+
# <p><em>Hello</em> world!</p>
25697
```
25798

25899
## Developing locally
@@ -264,7 +105,7 @@ script/bootstrap
264105
bundle exec rake compile
265106
```
266107

267-
If there were no errors, you're done! Otherwise, make sure to follow the CMark dependency instructions.
108+
If there were no errors, you're done! Otherwise, make sure to follow the comrak dependency instructions.
268109

269110
## Benchmarks
270111

@@ -273,16 +114,21 @@ Some rough benchmarks:
273114
```
274115
$ bundle exec rake benchmark
275116
276-
input size = 11063727 bytes
117+
input size = 11064832 bytes
277118
278-
redcarpet
279-
0.070000 0.020000 0.090000 ( 0.079641)
280-
github-markdown
281-
0.070000 0.010000 0.080000 ( 0.083535)
119+
Warming up --------------------------------------
120+
redcarpet 2.000 i/100ms
121+
commonmarker with to_html
122+
1.000 i/100ms
123+
kramdown 1.000 i/100ms
124+
Calculating -------------------------------------
125+
redcarpet 22.634 (± 4.4%) i/s - 114.000 in 5.054490s
282126
commonmarker with to_html
283-
0.100000 0.010000 0.110000 ( 0.111947)
284-
commonmarker with ruby HtmlRenderer
285-
1.830000 0.030000 1.860000 ( 1.866203)
286-
kramdown
287-
4.610000 0.070000 4.680000 ( 4.678398)
127+
7.340 (± 0.0%) i/s - 37.000 in 5.058352s
128+
kramdown 0.343 (± 0.0%) i/s - 2.000 in 5.834208s
129+
130+
Comparison:
131+
redcarpet: 22.6 i/s
132+
commonmarker with to_html: 7.3 i/s - 3.08x (± 0.00) slower
133+
kramdown: 0.3 i/s - 66.02x (± 0.00) slower
288134
```

0 commit comments

Comments
 (0)