Browse files

Initial commit

  • Loading branch information...
1 parent 5a45287 commit cc22f34a7e1354f5408cd87d6d32abfedf802f51 @jameswilding committed Oct 16, 2009
Showing with 252 additions and 28 deletions.
  1. +41 −0 README.markdown
  2. +0 −18 README.rdoc
  3. +13 −7 Rakefile
  4. +45 −0 bin/markup
  5. +38 −0 lib/markup.rb
  6. +42 −0 markup.gemspec
  7. +72 −3 test/markup_test.rb
  8. 0 test/output.html
  9. +1 −0 test/test_helper.rb
View
41 README.markdown
@@ -0,0 +1,41 @@
+# markup
+
+Markup is a small (ie tiny) command line tool for converting Markdown to HTML.
+
+## Installation
+
+ $ sudo gem install markup --source http://gemcutter.org
+
+## Usages
+
+ $ cd ~/projects/markup && ls
+ $ LICENSE README.markdown Rakefile VERSION bin lib markup.gemspec test
+ $ markup README.markdown
+ Created README.html
+ $ cat README.html
+ <h1>markup</h1>
+
+ <p>Markup is a small.... etc
+
+By default markup outputs to {sourcefile_name}.html, but you can specify a different filename using the -o option:
+
+ $ markup README.markdown -o instructions.html
+
+Overwrite existing output using the -F option (this will clobber existing files so be careful):
+
+ $ markup README.markdown -F
+
+## Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but
+ bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+## Copyright
+
+Copyright (c) 2009 James Wilding. See LICENSE for details.
View
18 README.rdoc
@@ -1,18 +0,0 @@
-= markup
-
-Description goes here.
-
-== Note on Patches/Pull Requests
-
-* Fork the project.
-* Make your feature addition or bug fix.
-* Add tests for it. This is important so I don't break it in a
- future version unintentionally.
-* Commit, do not mess with rakefile, version, or history.
- (if you want to have your own version, that is fine but
- bump version in a commit by itself I can ignore when I pull)
-* Send me a pull request. Bonus points for topic branches.
-
-== Copyright
-
-Copyright (c) 2009 James Wilding. See LICENSE for details.
View
20 Rakefile
@@ -4,13 +4,19 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
- gem.name = "markup"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
- gem.email = "james@jameswilding.net"
- gem.homepage = "http://github.com/jameswilding/markup"
- gem.authors = ["James Wilding"]
+ gem.name = "markup"
+ gem.summary = %Q{Command line Markdown-to-HTML conversion}
+ gem.description = %Q{Small command line tool that reads Markdown files and outputs HTML}
+
+ gem.email = "james@jameswilding.net"
+ gem.homepage = "http://github.com/jameswilding/markup"
+ gem.authors = ["James Wilding"]
+
+ gem.files = Dir['lib/**/*'] + Dir['bin/*']
+ gem.executables << 'markup'
+
gem.add_development_dependency "thoughtbot-shoulda"
+ gem.add_development_dependency "redgreen"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
rescue LoadError
@@ -37,7 +43,7 @@ rescue LoadError
end
end
-task :test => :check_dependencies
+task :test
task :default => :test
View
45 bin/markup
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+require 'optparse'
+
+# Command line tool for producing HTML from Markdown files.
+# Requires rubygems and RDiscount (gem install rdiscount).
+#
+# Usage: markup source_file.markdown [-o output_file] [-F]
+#
+# For help, use markup --help
+
+options = {}
+script = File.basename($0)
+
+OptionParser.new do |opts|
+ opts.banner = "Usage: #{script} source_file [options]"
+
+ opts.on '-o', '--output', 'Output file [FILE]' do |file|
+ options[:output] = file
+ end
+
+ opts.on '-F', '--force', 'Overwrite output file' do |force|
+ options[:force] = force
+ end
+end.parse!
+
+unless input = ARGV.first
+ puts "Please specify a source file"
+ exit 1
+end
+
+begin
+ require 'rubygems'
+ require File.dirname(__FILE__) + '/../lib/markup'
+
+ runner = Markup::Runner.new(input, options)
+ output = runner.output_filename
+
+ runner.generate_html!
+rescue Markup::Runner::OutputExists
+ puts "Output file #{output} exists: use #{script} -o to specify another"
+ exit 1
+else
+ puts "Created #{output}"
+ exit 0
+end
View
38 lib/markup.rb
@@ -0,0 +1,38 @@
+require 'rdiscount'
+
+module Markup
+ class Runner
+
+ class OutputExists < StandardError; end
+
+ def initialize(source, options = {})
+ @source = source
+ @options = options
+ end
+
+ def generate_html!
+ if output_file_exists? && should_not_overwrite_output_file?
+ raise OutputExists, output_filename
+ else
+ File.open(output_filename, 'w') { |f| f.write(html_output) }
+ end
+ end
+
+ def output_filename
+ @options[:output] || File.basename(@source, '.*') + '.html'
+ end
+
+ private
+ def html_output
+ RDiscount.new(File.read(@source)).to_html
+ end
+
+ def output_file_exists?
+ File.file?(output_filename)
+ end
+
+ def should_not_overwrite_output_file?
+ !@options[:force]
+ end
+ end
+end
View
42 markup.gemspec
@@ -0,0 +1,42 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{markup}
+ s.version = "0.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["James Wilding"]
+ s.date = %q{2009-10-16}
+ s.description = %q{Small command line that reads Markdown files and outputs HTML}
+ s.email = %q{james@jameswilding.net}
+ s.executables = ["markup", "markup"]
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ "bin/markup"
+ ]
+ s.homepage = %q{http://github.com/jameswilding/markup}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Command line Markdown-to-HTML conversion}
+ s.test_files = [
+ "test/markup_test.rb",
+ "test/test_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ else
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ end
+end
View
75 test/markup_test.rb
@@ -1,7 +1,76 @@
require 'test_helper'
+require 'fileutils'
class MarkupTest < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+
+ include FileUtils
+
+ def setup
+ @source = File.dirname(__FILE__) + '/../README.markdown'
+ @output = File.dirname(__FILE__) + '/output.html'
+ clear_output_file!
end
-end
+
+ context '#output_filename' do
+ context 'with no options' do
+ should 'set output filename to input filename + "html"' do
+ runner = Markup::Runner.new(@source)
+ expected = 'README.html'
+ actual = runner.output_filename
+
+ assert_equal expected, actual
+ end
+ end
+
+ context 'with options[:output] set' do
+ should 'use options[:output] as output filename' do
+ runner = Markup::Runner.new(@source, :output => 'instructions.html')
+ expected = 'instructions.html'
+ actual = runner.output_filename
+
+ assert_equal expected, actual
+ end
+ end
+ end
+
+ context '#generate_html!' do
+ context 'when options[:force] != true' do
+ should 'raise an exception if the output file exists' do
+ assert_raise(Markup::Runner::OutputExists) do
+ Markup::Runner.new(@source, :output => @output).generate_html!
+ end
+ end
+
+ should 'create output file if it does not exist' do
+ new_output = File.dirname(__FILE__) + '/new_output.html'
+ assert !File.file?(new_output)
+
+ Markup::Runner.new(@source, :output => new_output).generate_html!
+ assert File.file?(new_output)
+
+ rm(new_output) if File.file?(new_output)
+ end
+ end
+
+ context 'when options[:force] == true' do
+ should 'raise no exceptions, even when output file exists' do
+ assert_nothing_raised do
+ Markup::Runner.new(@source, :output => @output, :force => true).generate_html!
+ end
+ end
+
+ should 'overwrite contents of output file if it exists' do
+ old_contents = File.read(@output)
+ Markup::Runner.new(@source, :output => @output, :force => true).generate_html!
+ new_contents = File.read(@output)
+
+ assert new_contents != old_contents
+ assert new_contents =~ /^<h1>/, "New content doesn't seem to contain any HTML"
+ end
+ end
+ end
+
+ def clear_output_file!
+ File.open(@output, 'w') { |f| f.write nil }
+ end
+end
View
0 test/output.html
No changes.
View
1 test/test_helper.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'test/unit'
require 'shoulda'
+require 'redgreen'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

0 comments on commit cc22f34

Please sign in to comment.