literati-master/0000755000175000017500000000000012107731742014352 5ustar avtobiffavtobiffliterati-master/bin/0000755000175000017500000000000012107731742015122 5ustar avtobiffavtobiffliterati-master/bin/literati0000755000175000017500000000033212107731742016663 0ustar avtobiffavtobiff#!/usr/bin/env ruby $:.unshift File.dirname(__FILE__) + '/../lib' if ($0 == __FILE__) require 'literati' if ARGV.empty? abort "I need a filename to render!" else print Literati.render(File.read(ARGV.first)) end literati-master/README.md0000644000175000017500000000351412107731742015634 0ustar avtobiffavtobiffLiterati ======== Render literate Haskell into HTML using Ruby and magic. But mostly Ruby. Literati.render("Markdown here\n\n> your literate Haskell here\n\nMore Markdown.") # =>

Markdown here

your literate Haskell here
          

More Markdown.

Simple and straightforward! 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. An example `RedCarpet` wrapper would look like this: # A simple class to wrap passing the right arguments to RedCarpet. class RedCarpetRenderer # Create a new compatibility instance. # # content - The Markdown content to render. def initialize(content) require 'redcarpet/compat' @content = content end # Render the Markdown content to HTML. We use GFM-esque options here. # # Returns an HTML string. def to_html Markdown.new(@content, :fenced_code, :safelink, :autolink).to_html end end You can easily 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: renderer = Literati::Renderer.new("content", RSTRenderer) renderer.to_html 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. Lastly, you can use the simple binscript we included: literati file.lhs That'll pipe the HTML to `stdout` so you can direct it wherever. literati-master/literati.gemspec0000644000175000017500000000522012107731742017533 0ustar avtobiffavtobiff## This is the rakegem gemspec template. Make sure you read and understand ## all of the comments. Some sections require modification, and others can ## be deleted if you don't need them. Once you understand the contents of ## this file, feel free to delete any comments that begin with two hash marks. ## You can find comprehensive Gem::Specification documentation, at ## http://docs.rubygems.org/read/chapter/20 Gem::Specification.new do |s| s.specification_version = 2 if s.respond_to? :specification_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.rubygems_version = '1.3.5' ## Leave these as is they will be modified for you by the rake gemspec task. ## If your rubyforge_project name is different, then edit it and comment out ## the sub! line in the Rakefile s.name = 'literati' s.version = '0.0.4' ## Make sure your summary is short. The description may be as long ## as you like. s.summary = "Render literate Haskell with Ruby." s.description = "Render literate Haskell with Ruby for great good." ## List the primary authors. If there are a bunch of authors, it's probably ## better to set the email to an email list or something. If you don't have ## a custom homepage, consider using your GitHub URL or the like. s.authors = ["Jeremy McAnally"] s.email = 'jeremy@github.com' s.homepage = 'http://github.com/jm/literati' ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb' s.require_paths = %w[lib] ## If your gem includes any executables, list them here. s.executables = ["literati"] ## Specify any RDoc options here. You'll want to add your README and ## LICENSE files to the extra_rdoc_files list. s.rdoc_options = ["--charset=UTF-8"] s.extra_rdoc_files = %w[README.md LICENSE] ## List your development dependencies here. Development dependencies are ## those that are only needed during development s.add_development_dependency('contest') ## Leave this section as-is. It will be automatically generated from the ## contents of your Git repository via the gemspec task. DO NOT REMOVE ## THE MANIFEST COMMENTS, they are used as delimiters by the task. # = MANIFEST = s.files = %w[ LICENSE README.md Rakefile bin/literati lib/literati.rb literati.gemspec test/test_literati.rb ] # = MANIFEST = ## Test files will be grabbed from the file list. Make sure the path glob ## matches what you actually use. s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ } end literati-master/LICENSE0000644000175000017500000000205712107731742015363 0ustar avtobiffavtobiffThe MIT License Copyright (c) Jeremy McAnally 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. literati-master/lib/0000755000175000017500000000000012107731742015120 5ustar avtobiffavtobiffliterati-master/lib/literati.rb0000644000175000017500000001036112107731742017263 0ustar avtobiffavtobiffmodule Literati VERSION = '0.0.4' # Render the given content to HTML. # # content - Literate Haskell content to render to HTML # # Returns the literate Haskell rendered as HTML. def self.render(content) Renderer.new(content).to_html end # A simple class to wrap passing the right arguments to RedCarpet. class MarkdownRenderer class GitHubWrapper def initialize(content) @content = content end def to_html GitHub::Markdown.render(@content) end end # Create a new compatibility instance. # # content - The Markdown content to render. def initialize(content) @content = content end def determine_markdown_renderer @markdown = if installed?('github/markdown') GitHubWrapper.new(@content) elsif installed?('redcarpet/compat') Markdown.new(@content, :fenced_code, :safelink, :autolink) elsif installed?('redcarpet') RedcarpetCompat.new(@content) elsif installed?('rdiscount') RDiscount.new(@content) elsif installed?('maruku') Maruku.new(@content) elsif installed?('kramdown') Kramdown::Document.new(@content) elsif installed?('bluecloth') BlueCloth.new(@content) end end def installed?(file) begin require file true rescue LoadError false end end # Render the Markdown content to HTML. We use GFM-esque options here. # # Returns an HTML string. def to_html determine_markdown_renderer @markdown.to_html end end class Renderer # The Markdown class we're using to render HTML; is our # RedCarpet wrapped by default. attr_accessor :markdown_class # Regex used to determine presence of Bird-style comments BIRD_TRACKS_REGEX = /^>(--| )(.*)/ # Initialize a new literate Haskell renderer. # # content - The literate Haskell code string # markdowner - The class we'll use to render the HTML (defaults # to our RedCarpet wrapper). def initialize(content, markdowner = MarkdownRenderer) @bare_content = content @markdown = to_markdown @markdown_class = markdowner end # Render the given literate Haskell to a Markdown string. # # Returns a Markdown string we can render to HTML. def to_markdown lines = @bare_content.split("\n") markdown = "" # Using `while` here so we can alter the collection at will while current_line = lines.shift # If we got us some of them bird tracks... if current_line =~ BIRD_TRACKS_REGEX # Remove the bird tracks from this line current_line = remove_bird_tracks(current_line) # Grab the remaining code block current_line << slurp_remaining_bird_tracks(lines) # Fence it and add it to the output markdown << "```haskell\n#{current_line}\n```\n" else # No tracks? Just stick it back in the pile. markdown << current_line + "\n" end end markdown end # Remove Bird-style comment markers from a line of text. # # comment = "> Haskell codes" # remove_bird_tracks(comment) # # => "Haskell codes" # # Returns the given line of text sans bird tracks. def remove_bird_tracks(line) tracks = line.scan(BIRD_TRACKS_REGEX)[0] (tracks.first == " ") ? tracks[1] : tracks.join end # Given an Array of lines, pulls from the front of the Array # until the next line doesn't match our bird tracks regex. # # lines = ["> code", "> code", "", "not code"] # slurp_remaining_bird_tracks(lines) # # => "code\ncode" # # Returns the lines mashed into a string separated by a newline. def slurp_remaining_bird_tracks(lines) tracked_lines = [] while lines.first =~ BIRD_TRACKS_REGEX tracked_lines << remove_bird_tracks(lines.shift) end if tracked_lines.empty? "" else "\n" + tracked_lines.join("\n") end end # Render the Markdown string into HTML using the previously # specified Markdown renderer class. # # Returns an HTML string. def to_html @markdown_class.new(@markdown).to_html end end endliterati-master/Rakefile0000644000175000017500000000726512107731742016031 0ustar avtobiffavtobiffrequire 'rubygems' require 'rake' require 'date' ############################################################################# # # Helper functions # ############################################################################# def name @name ||= Dir['*.gemspec'].first.split('.').first end def version line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/] line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1] end def date Date.today.to_s end def rubyforge_project name end def gemspec_file "#{name}.gemspec" end def gem_file "#{name}-#{version}.gem" end def replace_header(head, header_name) head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"} end ############################################################################# # # Standard tasks # ############################################################################# task :default => :test require 'rake/testtask' Rake::TestTask.new(:test) do |test| test.libs << 'lib' << 'test' test.pattern = 'test/**/test_*.rb' test.verbose = true end desc "Generate RCov test coverage and open in your browser" task :coverage do require 'rcov' sh "rm -fr coverage" sh "rcov test/test_*.rb" sh "open coverage/index.html" end require 'rdoc/task' Rake::RDocTask.new do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = "#{name} #{version}" rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') end desc "Open an irb session preloaded with this library" task :console do sh "irb -rubygems -r ./lib/#{name}.rb" end ############################################################################# # # Custom tasks (add your own tasks here) # ############################################################################# ############################################################################# # # Packaging tasks # ############################################################################# desc "Create tag v#{version} and build and push #{gem_file} to Rubygems" task :release => :build do unless `git branch` =~ /^\* master$/ puts "You must be on the master branch to release!" exit! end sh "git commit --allow-empty -a -m 'Release #{version}'" sh "git tag v#{version}" sh "git push origin master" sh "git push origin v#{version}" sh "gem push pkg/#{name}-#{version}.gem" end desc "Build #{gem_file} into the pkg directory" task :build => :gemspec do sh "mkdir -p pkg" sh "gem build #{gemspec_file}" sh "mv #{gem_file} pkg" end desc "Generate #{gemspec_file}" task :gemspec => :validate do # read spec file and split out manifest section spec = File.read(gemspec_file) head, manifest, tail = spec.split(" # = MANIFEST =\n") # replace name version and date replace_header(head, :name) replace_header(head, :version) replace_header(head, :date) #comment this out if your rubyforge_project has a different name replace_header(head, :rubyforge_project) # determine file list from git ls-files files = `git ls-files`. split("\n"). sort. reject { |file| file =~ /^\./ }. reject { |file| file =~ /^(rdoc|pkg)/ }. map { |file| " #{file}" }. join("\n") # piece file back together and write manifest = " s.files = %w[\n#{files}\n ]\n" spec = [head, manifest, tail].join(" # = MANIFEST =\n") File.open(gemspec_file, 'w') { |io| io.write(spec) } puts "Updated #{gemspec_file}" end desc "Validate #{gemspec_file}" task :validate do libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"] unless libfiles.empty? puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir." exit! end unless Dir['VERSION*'].empty? puts "A `VERSION` file at root level violates Gem best practices." exit! end end literati-master/test/0000755000175000017500000000000012107731742015331 5ustar avtobiffavtobiffliterati-master/test/test_literati.rb0000644000175000017500000000467512107731742020546 0ustar avtobiffavtobiffrequire 'rubygems' require 'fileutils' require 'contest' require 'test/unit' require 'mocha' require "#{File.expand_path(File.dirname(__FILE__))}/../lib/literati.rb" TEST_CONTENT = "Hello there. > Haskell code > I have no clue what I'm doing. > Syntax! :: Yeah! -> CURRYING. More *Markdown*..." TEST_CONTENT_WITH_COMMENT = "Well this is convenient. >-- A comment, mi lord. > WHAT? WHERE??? Mo' content." class DummyRenderer def initialize(content) @content = content end def to_html @content end end class LiteratiTest < Test::Unit::TestCase context "Markdown rendering" do setup do @renderer = Literati::Renderer.new(TEST_CONTENT) end test "renders to Markdown string" do assert_match /\`\`\`haskell/m, @renderer.to_markdown end test "removes bird tracks" do assert_equal "more haskell codes", @renderer.remove_bird_tracks("> more haskell codes") end test "slurps remaining block properly" do assert_equal "\nline one\nline two\nline three", @renderer.slurp_remaining_bird_tracks(["> line one", "> line two", "> line three", ""]) end end context "Markdown rendering with comments" do setup do @renderer = Literati::Renderer.new(TEST_CONTENT_WITH_COMMENT) end test "renders to Markdown string" do assert_match /\`\`\`haskell/m, @renderer.to_markdown end test "removes bird tracks" do assert_equal "-- a wild comment appears!", @renderer.remove_bird_tracks(">-- a wild comment appears!") end test "slurps remaining block properly" do assert_equal "\n-- line one\nline two\nline three", @renderer.slurp_remaining_bird_tracks([">-- line one", "> line two", "> line three", ""]) end test "slurps remaining block properly with multiple comment lines" do assert_equal "\n-- line one\n--line two\nline three\n-- more commenting...", @renderer.slurp_remaining_bird_tracks([">-- line one", ">--line two", "> line three", ">-- more commenting...", ""]) end end context "HTML rendering" do test "renders to HTML using our Smart Renderer(tm) by default" do Literati::MarkdownRenderer.any_instance.expects(:to_html) Literati.render("markdown\n\n> codes\n\nmoar markdown") end test "can use other Markdown class" do DummyRenderer.any_instance.expects(:to_html) renderer = Literati::Renderer.new("markdown\n\n> codes\n\nmoar markdown", DummyRenderer) renderer.to_html end end end