hypertextmarkdown 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +30 -0
- data/Rakefile +6 -0
- data/Readme.md +15 -0
- data/hypertextmarkdown.gemspec +12 -0
- data/lib/hypertextmarkdown.rb +93 -0
- data/lib/hypertextmarkdown/version.rb +3 -0
- data/spec/hypertextmarkdown_spec.rb +36 -0
- data/spec/spec_helper.rb +1 -0
- metadata +55 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
spec/fixtures.yml
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hypertextmarkdown (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
bump (0.3.5)
|
10
|
+
diff-lcs (1.1.3)
|
11
|
+
nokogiri (1.5.2)
|
12
|
+
rake (0.9.2.2)
|
13
|
+
rspec (2.11.0)
|
14
|
+
rspec-core (~> 2.11.0)
|
15
|
+
rspec-expectations (~> 2.11.0)
|
16
|
+
rspec-mocks (~> 2.11.0)
|
17
|
+
rspec-core (2.11.1)
|
18
|
+
rspec-expectations (2.11.3)
|
19
|
+
diff-lcs (~> 1.1.3)
|
20
|
+
rspec-mocks (2.11.3)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
bump
|
27
|
+
hypertextmarkdown!
|
28
|
+
nokogiri
|
29
|
+
rake
|
30
|
+
rspec (~> 2)
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# HyperTextMarkDown
|
2
|
+
|
3
|
+
Convert html to markdown
|
4
|
+
|
5
|
+
Usage
|
6
|
+
=====
|
7
|
+
|
8
|
+
markdown = HyperTextMarkDown::to_markdown("<b>Goodbye world</b>")
|
9
|
+
|
10
|
+
|
11
|
+
Author
|
12
|
+
======
|
13
|
+
[Jonathan Cheatham](http://github.com/jcheatham)<br/>
|
14
|
+
coaxis@gmail.com<br/>
|
15
|
+
License: MIT
|
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
2
|
+
name = "hypertextmarkdown"
|
3
|
+
require "#{name}/version"
|
4
|
+
|
5
|
+
Gem::Specification.new name, HyperTextMarkDown::VERSION do |s|
|
6
|
+
s.summary = "HTML to markdown converter"
|
7
|
+
s.authors = ["Jonathan Cheatham"]
|
8
|
+
s.email = "coaxis@gmail.com"
|
9
|
+
s.homepage = "http://github.com/jcheatham/#{name}"
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.license = "MIT"
|
12
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'hypertextmarkdown/version'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module HyperTextMarkDown
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def to_markdown(str)
|
10
|
+
html = Nokogiri::HTML(str)
|
11
|
+
# pre-processing / xpath selection here
|
12
|
+
html.children.map{|child| mark_element(child)}.join
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def mark_element(element, opts={})
|
18
|
+
#puts "\nmark_element(#{element.inspect},#{opts.inspect})"
|
19
|
+
if element.is_a?(Nokogiri::XML::Text)
|
20
|
+
return "" if element.text.to_s.strip.empty?
|
21
|
+
return element.text
|
22
|
+
end
|
23
|
+
|
24
|
+
children = element.children
|
25
|
+
|
26
|
+
case element.name.downcase
|
27
|
+
|
28
|
+
when 'br'
|
29
|
+
" \n"
|
30
|
+
|
31
|
+
when 'hr'
|
32
|
+
"\n****\n"
|
33
|
+
|
34
|
+
when 'a'
|
35
|
+
"[#{children.map{|child| mark_element(child,opts)}.join}](#{element['href']})"
|
36
|
+
|
37
|
+
when 'img'
|
38
|
+
"![#{element['alt']}](#{element['src']})"
|
39
|
+
|
40
|
+
when 'em', 'i'
|
41
|
+
"*#{children.map{|child| mark_element(child,opts)}.join}*"
|
42
|
+
|
43
|
+
when 'strong', 'b'
|
44
|
+
"**#{children.map{|child| mark_element(child,opts)}.join}**"
|
45
|
+
|
46
|
+
when 'span'
|
47
|
+
"#{children.map{|child| mark_element(child,opts)}.join}"
|
48
|
+
|
49
|
+
when 'p'
|
50
|
+
"#{opts[:indent]}#{children.map{|child| mark_element(child,opts)}.join}\n\n"
|
51
|
+
|
52
|
+
when 'code'
|
53
|
+
"\n\n```\n#{children.map{|child| mark_element(child,opts)}.join}\n```\n"
|
54
|
+
|
55
|
+
when 'pre'
|
56
|
+
new_indent = "#{opts[:indent]} "
|
57
|
+
"#{opts[:indent]}#{children.map{|child| mark_element(child,opts.merge(:indent=>new_indent))}.join}\n"
|
58
|
+
|
59
|
+
when 'blockquote'
|
60
|
+
new_indent = "#{opts[:indent]} > "
|
61
|
+
"#{children.map{|child| mark_element(child,opts.merge(:indent=>new_indent))}.join}"
|
62
|
+
|
63
|
+
when 'h1'
|
64
|
+
"# #{children.map{|child| mark_element(child,opts)}.join}\n"
|
65
|
+
when 'h2'
|
66
|
+
"## #{children.map{|child| mark_element(child,opts)}.join}\n"
|
67
|
+
when 'h3'
|
68
|
+
"### #{children.map{|child| mark_element(child,opts)}.join}\n"
|
69
|
+
|
70
|
+
when 'ul'
|
71
|
+
spacer = opts[:list_type] ? "" : "\n"
|
72
|
+
"#{spacer}#{children.map{|child| mark_element(child,opts.merge(:list_type=>:ul))}.join}"
|
73
|
+
|
74
|
+
when 'ol'
|
75
|
+
spacer = opts[:list_type] ? "" : "\n"
|
76
|
+
"#{spacer}#{children.map{|child| mark_element(child,opts.merge(:list_type=>:ol))}.join}"
|
77
|
+
|
78
|
+
when 'li'
|
79
|
+
li_prefix = (opts[:list_type] == :ol) ? " - " : " 1. "
|
80
|
+
new_indent = "#{opts[:indent]} "
|
81
|
+
"#{opts[:indent]}#{li_prefix}#{children.map{|child| mark_element(child,opts.merge(:indent=>new_indent))}.join}\n"
|
82
|
+
|
83
|
+
when 'script','style'
|
84
|
+
""
|
85
|
+
|
86
|
+
else
|
87
|
+
"#{children.map{|child| mark_element(child,opts)}.join}"
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'hypertextmarkdown'
|
2
|
+
|
3
|
+
ROOT = File.expand_path('../../', __FILE__)
|
4
|
+
|
5
|
+
describe "hypertextmarkdown" do
|
6
|
+
describe "convert html to markdown" do
|
7
|
+
|
8
|
+
it "handles a basic example" do
|
9
|
+
HyperTextMarkDown::to_markdown("1 how's <strong>IT</strong> going?").should == "1 how's **IT** going?\n\n"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "ignores unknown tags" do
|
13
|
+
HyperTextMarkDown::to_markdown("2 how's <string>IT</string> going?").should == "2 how's IT going?\n\n"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "kinda handles a broken tag" do
|
17
|
+
HyperTextMarkDown::to_markdown("3 how's <strong IT</strong> going?").should == "3 how's ** going?**\n\n"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "kinda handles a different broken tag" do
|
21
|
+
HyperTextMarkDown::to_markdown("4 how's <strong>IT</stro\nng> going?").should == "4 how's **IT going?**\n\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "converts a reasonable sample" do
|
25
|
+
html = "<html><body><div class=\"mail_main\"><span>Example</span><hr /><h3>Contact form</h3>\nHello<br />\n<ol>\n<li>Contact Info:\n<ul>\n<li>Name: <strong>Joe Somebody</strong></li>\n<li>E-mail: <a href=\"mailto:somebody@example.com\">somebody@example.com</a></li><li>Code sample: <code>\n#include <stdio.h>\n\nint main(int argc, char** argv) {\nprintf(\"blah\");\nreturn 0;\n}\n</code></li>\n<ul>\n</li>\n<li>Supplemental Info:\n<ul>\n<li>Favorite color: <span>teal</span></li>\n<li>Favorite image: <img alt=\"Fun\" src=\"http://upload.wikimedia.org/wikipedia/commons/e/ec/Happy_smiley_face.png\"/></li>\n</ul>\n</li>\n</ol>\n</div>\n</body></html>"
|
26
|
+
|
27
|
+
expected = "Example\n****\n### Contact form\n\nHello \n\n - Contact Info:\n 1. Name: **Joe Somebody**\n 1. E-mail: [somebody@example.com](mailto:somebody@example.com)\n 1. Code sample: \n\n```\n\n#include <stdio.h>\n\nint main(int argc, char** argv) {\nprintf(\"blah\");\nreturn 0;\n}\n\n```\n\n\n - Supplemental Info:\n 1. Favorite color: teal\n 1. Favorite image: \n\n"
|
28
|
+
|
29
|
+
markdown = HyperTextMarkDown::to_markdown(html)
|
30
|
+
|
31
|
+
markdown.should == expected
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "hypertextmarkdown"
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hypertextmarkdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan Cheatham
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-10 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: coaxis@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- Rakefile
|
24
|
+
- Readme.md
|
25
|
+
- hypertextmarkdown.gemspec
|
26
|
+
- lib/hypertextmarkdown.rb
|
27
|
+
- lib/hypertextmarkdown/version.rb
|
28
|
+
- spec/hypertextmarkdown_spec.rb
|
29
|
+
- spec/spec_helper.rb
|
30
|
+
homepage: http://github.com/jcheatham/hypertextmarkdown
|
31
|
+
licenses:
|
32
|
+
- MIT
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.23
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: HTML to markdown converter
|
55
|
+
test_files: []
|