literati 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Tom Preston-Werner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ Literati
2
+ ========
3
+
4
+ Render literate Haskell into HTML using Ruby and magic. But mostly Ruby.
5
+
6
+ Literati.render("Markdown here\n\n> your literate Haskell here\n\nMore Markdown.")
7
+ # => <p>Markdown here</p>
8
+
9
+ <pre><code class="haskell">your literate Haskell here
10
+ </code></pre>
11
+
12
+ <p>More Markdown.</p>
13
+
14
+ Simple and straightforward!
15
+
16
+ By default, we render using Markdown. If you want to use another markup language or Markdown renderer, then you can use the extra magical extended API. The only requirement is that the class takes the content as the sole argument for the initializer and exposes a `to_html` method. Our `RedCarpet` wrapper looks like this:
17
+
18
+ # A simple class to wrap passing the right arguments to RedCarpet.
19
+ class RedCarpetRenderer
20
+ # Create a new compatibility instance.
21
+ #
22
+ # content - The Markdown content to render.
23
+ def initialize(content)
24
+ require 'redcarpet/compat'
25
+ @content = content
26
+ end
27
+
28
+ # Render the Markdown content to HTML. We use GFM-esque options here.
29
+ #
30
+ # Returns an HTML string.
31
+ def to_html
32
+ Markdown.new(@content, :fenced_code, :safelink, :autolink).to_html
33
+ end
34
+ end
35
+
36
+ You can use that as a base for other wrappers. If you wanted to use, for example, a `reStructuredText` wrapper of some sort with `literati`, you'd do something like this:
37
+
38
+ renderer = Literati::Renderer.new("content", RSTRenderer)
39
+ renderer.to_html
40
+
41
+ The second, optional argument to the `Renderer` class's initializer is the class (i.e., *not* an instance) that you'll use to build the HTML. If you leave that option off, it'll default to our `RedCarpetRenderer` class.
42
+
43
+ Lastly, you can use the simple binscript we included:
44
+
45
+ literati file.lhs
46
+
47
+ That'll pipe the HTML to `stdout` so you can direct it wherever.
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'date'
4
+
5
+ #############################################################################
6
+ #
7
+ # Helper functions
8
+ #
9
+ #############################################################################
10
+
11
+ def name
12
+ @name ||= Dir['*.gemspec'].first.split('.').first
13
+ end
14
+
15
+ def version
16
+ line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
17
+ line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
18
+ end
19
+
20
+ def date
21
+ Date.today.to_s
22
+ end
23
+
24
+ def rubyforge_project
25
+ name
26
+ end
27
+
28
+ def gemspec_file
29
+ "#{name}.gemspec"
30
+ end
31
+
32
+ def gem_file
33
+ "#{name}-#{version}.gem"
34
+ end
35
+
36
+ def replace_header(head, header_name)
37
+ head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
38
+ end
39
+
40
+ #############################################################################
41
+ #
42
+ # Standard tasks
43
+ #
44
+ #############################################################################
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/testtask'
49
+ Rake::TestTask.new(:test) do |test|
50
+ test.libs << 'lib' << 'test'
51
+ test.pattern = 'test/**/test_*.rb'
52
+ test.verbose = true
53
+ end
54
+
55
+ desc "Generate RCov test coverage and open in your browser"
56
+ task :coverage do
57
+ require 'rcov'
58
+ sh "rm -fr coverage"
59
+ sh "rcov test/test_*.rb"
60
+ sh "open coverage/index.html"
61
+ end
62
+
63
+ require 'rake/rdoctask'
64
+ Rake::RDocTask.new do |rdoc|
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "#{name} #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
70
+
71
+ desc "Open an irb session preloaded with this library"
72
+ task :console do
73
+ sh "irb -rubygems -r ./lib/#{name}.rb"
74
+ end
75
+
76
+ #############################################################################
77
+ #
78
+ # Custom tasks (add your own tasks here)
79
+ #
80
+ #############################################################################
81
+
82
+
83
+
84
+ #############################################################################
85
+ #
86
+ # Packaging tasks
87
+ #
88
+ #############################################################################
89
+
90
+ desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
91
+ task :release => :build do
92
+ unless `git branch` =~ /^\* master$/
93
+ puts "You must be on the master branch to release!"
94
+ exit!
95
+ end
96
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
97
+ sh "git tag v#{version}"
98
+ sh "git push origin master"
99
+ sh "git push origin v#{version}"
100
+ sh "gem push pkg/#{name}-#{version}.gem"
101
+ end
102
+
103
+ desc "Build #{gem_file} into the pkg directory"
104
+ task :build => :gemspec do
105
+ sh "mkdir -p pkg"
106
+ sh "gem build #{gemspec_file}"
107
+ sh "mv #{gem_file} pkg"
108
+ end
109
+
110
+ desc "Generate #{gemspec_file}"
111
+ task :gemspec => :validate do
112
+ # read spec file and split out manifest section
113
+ spec = File.read(gemspec_file)
114
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
115
+
116
+ # replace name version and date
117
+ replace_header(head, :name)
118
+ replace_header(head, :version)
119
+ replace_header(head, :date)
120
+ #comment this out if your rubyforge_project has a different name
121
+ replace_header(head, :rubyforge_project)
122
+
123
+ # determine file list from git ls-files
124
+ files = `git ls-files`.
125
+ split("\n").
126
+ sort.
127
+ reject { |file| file =~ /^\./ }.
128
+ reject { |file| file =~ /^(rdoc|pkg)/ }.
129
+ map { |file| " #{file}" }.
130
+ join("\n")
131
+
132
+ # piece file back together and write
133
+ manifest = " s.files = %w[\n#{files}\n ]\n"
134
+ spec = [head, manifest, tail].join(" # = MANIFEST =\n")
135
+ File.open(gemspec_file, 'w') { |io| io.write(spec) }
136
+ puts "Updated #{gemspec_file}"
137
+ end
138
+
139
+ desc "Validate #{gemspec_file}"
140
+ task :validate do
141
+ libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
142
+ unless libfiles.empty?
143
+ puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
144
+ exit!
145
+ end
146
+ unless Dir['VERSION*'].empty?
147
+ puts "A `VERSION` file at root level violates Gem best practices."
148
+ exit!
149
+ end
150
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.dirname(__FILE__) + '/../lib'
4
+
5
+ require 'literati'
6
+
7
+ if ARGV.empty?
8
+ puts "I need a filename to render!"
9
+ else
10
+ puts Literati.render(File.read(ARGV.first))
11
+ end
@@ -0,0 +1,120 @@
1
+ require 'rubygems'
2
+
3
+ module Literati
4
+ VERSION = '0.0.1'
5
+
6
+ # Render the given content to HTML.
7
+ #
8
+ # content - Literate Haskell content to render to HTML
9
+ #
10
+ # Returns the literate Haskell rendered as HTML.
11
+ def self.render(content)
12
+ Renderer.new(content).to_html
13
+ end
14
+
15
+ # A simple class to wrap passing the right arguments to RedCarpet.
16
+ class RedCarpetRenderer
17
+ # Create a new compatibility instance.
18
+ #
19
+ # content - The Markdown content to render.
20
+ def initialize(content)
21
+ require 'redcarpet/compat'
22
+ @content = content
23
+ end
24
+
25
+ # Render the Markdown content to HTML. We use GFM-esque options here.
26
+ #
27
+ # Returns an HTML string.
28
+ def to_html
29
+ Markdown.new(@content, :fenced_code, :safelink, :autolink).to_html
30
+ end
31
+ end
32
+
33
+ class Renderer
34
+ # The Markdown class we're using to render HTML; is our
35
+ # RedCarpet wrapped by default.
36
+ attr_accessor :markdown_class
37
+
38
+ # Regex used to determine presence of Bird-style comments
39
+ BIRD_TRACKS_REGEX = /^\> (.*)/
40
+
41
+ # Initialize a new literate Haskell renderer.
42
+ #
43
+ # content - The literate Haskell code string
44
+ # markdowner - The class we'll use to render the HTML (defaults
45
+ # to our RedCarpet wrapper).
46
+ def initialize(content, markdowner = RedCarpetRenderer)
47
+ @bare_content = content
48
+ @markdown = to_markdown
49
+ @markdown_class = markdowner
50
+ end
51
+
52
+ # Render the given literate Haskell to a Markdown string.
53
+ #
54
+ # Returns a Markdown string we can render to HTML.
55
+ def to_markdown
56
+ lines = @bare_content.split("\n")
57
+ markdown = ""
58
+
59
+ # Using `while` here so we can alter the collection at will
60
+ while current_line = lines.shift
61
+ # If we got us some of them bird tracks...
62
+ if current_line =~ BIRD_TRACKS_REGEX
63
+ # Remove the bird tracks from this line
64
+ current_line = remove_bird_tracks(current_line)
65
+ # Grab the remaining code block
66
+ current_line << slurp_remaining_bird_tracks(lines)
67
+
68
+ # Fence it and add it to the output
69
+ markdown << "```haskell\n#{current_line}\n```\n"
70
+ else
71
+ # No tracks? Just stick it back in the pile.
72
+ markdown << current_line + "\n"
73
+ end
74
+ end
75
+
76
+ markdown
77
+ end
78
+
79
+ # Remove Bird-style comment markers from a line of text.
80
+ #
81
+ # comment = "> Haskell codes"
82
+ # remove_bird_tracks(comment)
83
+ # # => "Haskell codes"
84
+ #
85
+ # Returns the given line of text sans bird tracks.
86
+ def remove_bird_tracks(line)
87
+ line.gsub(BIRD_TRACKS_REGEX, '\1')
88
+ end
89
+
90
+ # Given an Array of lines, pulls from the front of the Array
91
+ # until the next line doesn't match our bird tracks regex.
92
+ #
93
+ # lines = ["> code", "> code", "", "not code"]
94
+ # slurp_remaining_bird_tracks(lines)
95
+ # # => "code\ncode"
96
+ #
97
+ # Returns the lines mashed into a string separated by a newline.
98
+ def slurp_remaining_bird_tracks(lines)
99
+ tracked_lines = []
100
+
101
+ while lines.first =~ BIRD_TRACKS_REGEX
102
+ tracked_lines << remove_bird_tracks(lines.shift)
103
+ end
104
+
105
+ if tracked_lines.empty?
106
+ ""
107
+ else
108
+ "\n" + tracked_lines.join("\n")
109
+ end
110
+ end
111
+
112
+ # Render the Markdown string into HTML using the previously
113
+ # specified Markdown renderer class.
114
+ #
115
+ # Returns an HTML string.
116
+ def to_html
117
+ @markdown_class.new(@markdown).to_html
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,70 @@
1
+ ## This is the rakegem gemspec template. Make sure you read and understand
2
+ ## all of the comments. Some sections require modification, and others can
3
+ ## be deleted if you don't need them. Once you understand the contents of
4
+ ## this file, feel free to delete any comments that begin with two hash marks.
5
+ ## You can find comprehensive Gem::Specification documentation, at
6
+ ## http://docs.rubygems.org/read/chapter/20
7
+ Gem::Specification.new do |s|
8
+ s.specification_version = 2 if s.respond_to? :specification_version=
9
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
10
+ s.rubygems_version = '1.3.5'
11
+
12
+ ## Leave these as is they will be modified for you by the rake gemspec task.
13
+ ## If your rubyforge_project name is different, then edit it and comment out
14
+ ## the sub! line in the Rakefile
15
+ s.name = 'literati'
16
+ s.version = '0.0.1'
17
+ s.date = '2012-10-31'
18
+ s.rubyforge_project = 'literati'
19
+
20
+ ## Make sure your summary is short. The description may be as long
21
+ ## as you like.
22
+ s.summary = "Render literate Haskell with Ruby."
23
+ s.description = "Render literate Haskell with Ruby for great good."
24
+
25
+ ## List the primary authors. If there are a bunch of authors, it's probably
26
+ ## better to set the email to an email list or something. If you don't have
27
+ ## a custom homepage, consider using your GitHub URL or the like.
28
+ s.authors = ["Jeremy McAnally"]
29
+ s.email = 'jeremy@github.com'
30
+ s.homepage = 'http://github.com/jm/literati'
31
+
32
+ ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
+ ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
34
+ s.require_paths = %w[lib]
35
+
36
+ ## If your gem includes any executables, list them here.
37
+ s.executables = ["literati"]
38
+
39
+ ## Specify any RDoc options here. You'll want to add your README and
40
+ ## LICENSE files to the extra_rdoc_files list.
41
+ s.rdoc_options = ["--charset=UTF-8"]
42
+ s.extra_rdoc_files = %w[README.md LICENSE]
43
+
44
+ ## List your runtime dependencies here. Runtime dependencies are those
45
+ ## that are needed for an end user to actually USE your code.
46
+ s.add_dependency('redcarpet')
47
+
48
+ ## List your development dependencies here. Development dependencies are
49
+ ## those that are only needed during development
50
+ s.add_development_dependency('contest')
51
+
52
+ ## Leave this section as-is. It will be automatically generated from the
53
+ ## contents of your Git repository via the gemspec task. DO NOT REMOVE
54
+ ## THE MANIFEST COMMENTS, they are used as delimiters by the task.
55
+ # = MANIFEST =
56
+ s.files = %w[
57
+ LICENSE
58
+ README.md
59
+ Rakefile
60
+ bin/literati
61
+ lib/literati.rb
62
+ literati.gemspec
63
+ test/test_literati.rb
64
+ ]
65
+ # = MANIFEST =
66
+
67
+ ## Test files will be grabbed from the file list. Make sure the path glob
68
+ ## matches what you actually use.
69
+ s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
70
+ end
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'contest'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+
7
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/literati.rb"
8
+
9
+ TEST_CONTENT = "Hello there.
10
+
11
+ > Haskell code
12
+ > I have no clue what I'm doing.
13
+ > Syntax! :: Yeah! -> CURRYING.
14
+
15
+ More *Markdown*..."
16
+
17
+ class DummyRenderer
18
+ def initialize(content)
19
+ @content = content
20
+ end
21
+
22
+ def to_html
23
+ @content
24
+ end
25
+ end
26
+
27
+ class LiteratiTest < Test::Unit::TestCase
28
+ context "Markdown rendering" do
29
+ setup do
30
+ @renderer = Literati::Renderer.new(TEST_CONTENT)
31
+ end
32
+
33
+ test "renders to Markdown string" do
34
+ assert_match /\`\`\`haskell/m, @renderer.to_markdown
35
+ end
36
+
37
+ test "removes bird tracks" do
38
+ assert_equal "more haskell codes", @renderer.remove_bird_tracks("> more haskell codes")
39
+ end
40
+
41
+ test "slurps remaining block properly" do
42
+ assert_equal "\nline one\nline two\nline three", @renderer.slurp_remaining_bird_tracks(["> line one", "> line two", "> line three", ""])
43
+ end
44
+ end
45
+
46
+ context "HTML rendering" do
47
+ test "renders to HTML using RedCarpet by default" do
48
+ Literati::RedCarpetRenderer.any_instance.expects(:to_html)
49
+ Literati.render("markdown\n\n> codes\n\nmoar markdown")
50
+ end
51
+
52
+ test "RedCarpet options are turned on properly" do
53
+ assert_match /class=\"haskell\"/m, Literati.render("markdown\n\n> codes\n\nmoar markdown")
54
+ end
55
+
56
+ test "can use other Markdown class" do
57
+ DummyRenderer.any_instance.expects(:to_html)
58
+
59
+ renderer = Literati::Renderer.new("markdown\n\n> codes\n\nmoar markdown", DummyRenderer)
60
+ renderer.to_html
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: literati
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jeremy McAnally
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-31 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: redcarpet
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: contest
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Render literate Haskell with Ruby for great good.
50
+ email: jeremy@github.com
51
+ executables:
52
+ - literati
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - README.md
57
+ - LICENSE
58
+ files:
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - bin/literati
63
+ - lib/literati.rb
64
+ - literati.gemspec
65
+ - test/test_literati.rb
66
+ has_rdoc: true
67
+ homepage: http://github.com/jm/literati
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: literati
96
+ rubygems_version: 1.6.2
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: Render literate Haskell with Ruby.
100
+ test_files:
101
+ - test/test_literati.rb