Permalink
Browse files

Package for release

  • Loading branch information...
1 parent ef55786 commit 5648f23821b8f53721c07566a2a9ca48f0d7b56f @runpaint committed Jul 6, 2010
Showing with 737 additions and 29 deletions.
  1. +674 −20 LICENSE
  2. +9 −4 README.rdoc → README.md
  3. +1 −0 Rakefile
  4. +1 −1 VERSION
  5. +1 −0 fixtures/hex-literal.ebnf
  6. BIN fixtures/hex-literal.png
  7. +34 −0 lib/pngrammar.rb
  8. +17 −4 spec/pngrammar_spec.rb
View
Oops, something went wrong.
@@ -1,8 +1,13 @@
-= pngrammar
+# PNGrammar
-Description goes here.
+Trivial wrapper around [Automatic Generation of Syntax
+Diagrams](http://www-cgi.uni-regensburg.de/~brf09510/syntax.html) that I'm
+using for [Read Ruby](http://github.com/runpaint/read-ruby). Simply renders a
+given EBNF file as a PNG image. If the `convert` program (from ImageMagick) is
+in your path, it is used to resize the PNG; otherwise a large border will be
+present.
-== Note on Patches/Pull Requests
+## Note on Patches/Pull Requests
* Fork the project.
* Make your feature addition or bug fix.
@@ -12,6 +17,6 @@ Description goes here.
(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
Copyright (c) 2010 Run Paint Run Run. See LICENSE for details.
View
@@ -10,6 +10,7 @@ begin
gem.email = "runrun@runpaint.org"
gem.homepage = "http://github.com/runpaint/pngrammar"
gem.authors = ["Run Paint Run Run"]
+ gem.add_dependency "mechanize"
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_development_dependency "yard", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
View
@@ -1 +1 @@
-0.0.0
+0.0.1
@@ -0,0 +1 @@
+HexadecimalLiteral = Sign? "0" ("x" | "X") HexDigit+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,34 @@
+require 'tempfile'
+require 'mechanize'
+
+class PNGrammar
+ attr_reader :ebnf, :ua
+ URL = "http://www-cgi.uni-regensburg.de/~brf09510/syntax.html"
+ def initialize(file)
+ @ebnf = File.read(file).gsub(/\n/,' ')
+ @ua = Mechanize.new
+ end
+
+ def image
+ resize_image ua.get_file(results.at('img')['src'])
+ end
+
+ def results
+ @results ||= ua.get(URL).forms.first.tap do |f|
+ f['ssyntax'] = ebnf
+ f['width'] = 1000
+ end.submit.parser
+ end
+
+ def resize_image(png)
+ Tempfile.open('pngrammar') do |tempfile|
+ tempfile.print png
+ tempfile.close
+ return begin
+ `convert png:#{tempfile.path} -strip -trim -bordercolor White -border 10x10 -`
+ rescue Errno::ENOENT
+ tempfile.read
+ end.force_encoding('binary')
+ end
+ end
+end
@@ -1,7 +1,20 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require_relative 'spec_helper'
+require 'tempfile'
-describe "Pngrammar" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
+# ImageMagick adds metadata to images when resizing them, which foils our
+# test. Therefore, to normalise the file, we strip the two date fields.
+def strip_dates png
+ Tempfile.open('pngrammar') do |tempfile|
+ tempfile.print png
+ tempfile.close
+ return `convert +set date:create +set date:modify png:#{tempfile.path} -`.
+ force_encoding('binary')
+ end
+end
+
+describe PNGrammar do
+ it "renders EBNF to PNG" do
+ pg = PNGrammar.new('fixtures/hex-literal.ebnf')
+ strip_dates(pg.image).should == File.binread('fixtures/hex-literal.png')
end
end

0 comments on commit 5648f23

Please sign in to comment.