Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial support for pluggable parsers

parser management is done in `Prawn::Text::Formatted`.
i tried to keep everything as simple as possible!
  • Loading branch information...
commit fed495d581f570b5d3000f6d388f1bfb0ca36ad6 1 parent 4e10098
Christoph Schiessl authored
View
4 lib/prawn/font.rb
@@ -217,11 +217,11 @@ def font_families
# it and redefine the width calculation behavior.
#++
def width_of(string, options={})
- if options[:inline_format]
+ if p = options[:inline_format]
# Build up an Arranger with the entire string on one line, finalize it,
# and find its width.
arranger = Core::Text::Formatted::Arranger.new(self, options)
- arranger.consumed = Text::Formatted::Parser.to_array(string)
+ arranger.consumed = Text::Formatted.invoke_parser(p, string)
arranger.finalize_line
arranger.line_width
View
4 lib/prawn/table/cell/text.rb
@@ -105,11 +105,11 @@ def with_text_color
end
def text_box(extra_options={})
- if @text_options[:inline_format]
+ if p = @text_options[:inline_format]
options = @text_options.dup
options.delete(:inline_format)
- array = ::Prawn::Text::Formatted::Parser.to_array(@content)
+ array = ::Prawn::Text::Formatted.invoke_parser(p, @content)
::Prawn::Text::Formatted::Box.new(array,
options.merge(extra_options).merge(:document => @pdf))
else
View
4 lib/prawn/text.rb
@@ -156,9 +156,9 @@ def text(string, options={})
# we modify the options. don't change the user's hash
options = options.dup
- if options[:inline_format]
+ if p = options[:inline_format]
options.delete(:inline_format)
- array = Text::Formatted::Parser.to_array(string)
+ array = Text::Formatted.invoke_parser(p, string)
else
array = [{ :text => string }]
end
View
4 lib/prawn/text/box.rb
@@ -108,8 +108,8 @@ def text_box(string, options={})
options = options.dup
options[:document] = self
- box = if options.delete(:inline_format)
- array = Text::Formatted::Parser.to_array(string)
+ box = if p = options.delete(:inline_format)
+ array = Text::Formatted.invoke_parser(p, string)
Text::Formatted::Box.new(array, options)
else
Text::Box.new(string, options)
View
32 lib/prawn/text/formatted.rb
@@ -2,3 +2,35 @@
require "prawn/text/formatted/box"
require "prawn/text/formatted/parser"
require "prawn/text/formatted/fragment"
+
+module Prawn
+ module Text
+ module Formatted
+ def self.register_parser(name, parser)
+ @known_parsers ||= Hash.new
+ @known_parsers[name] = parser
+ end
+
+ def self.unregister_parser(name)
+ @known_parsers ||= Hash.new
+ @known_parsers[name] = nil
+ end
+
+ def self.find_parser(name)
+ @known_parsers ||= Hash.new
+ @known_parsers[name]
+ end
+
+ def self.invoke_parser(name, string)
+ extra_args = []
+
+ if name.is_a?(Array)
+ extra_args, name = name[1..-1], name.first
+ end
+
+ parser = self.find_parser(name) || Parser
+ return parser.to_array(string, *extra_args)
+ end
+ end
+ end
+end
View
35 spec/pluggable_parsers_spec.rb
@@ -0,0 +1,35 @@
+# encoding: utf-8
+
+require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
+
+require "prawn/text/formatted"
+
+describe "pluggable parsers" do
+ Formatted = Prawn::Text::Formatted
+
+ context "#register_parser, #unregister_parser, and #find_parser" do
+ it "finds registered parsers correctly" do
+ Formatted.register_parser(:mock, "mock parser")
+ Formatted.find_parser(:mock).should == "mock parser"
+ Formatted.unregister_parser(:mock)
+ Formatted.find_parser(:mock).should == nil
+ end
+ end
+
+ context "#invoke_parser" do
+ it "calls the parsers to_array method with corect arguments" do
+ class TestParser
+ def self.to_array(string, *args)
+ return [string, args]
+ end
+ end
+
+ Formatted.register_parser(:mock, TestParser)
+ Formatted.invoke_parser(:mock, "string").should == ["string", []]
+ Formatted.invoke_parser([:mock], "string").should == ["string", []]
+ Formatted.invoke_parser([:mock, "foo"], "string").should == ["string", ["foo"]]
+ Formatted.invoke_parser([:mock, "foo", "bar"], "string").should == ["string", ["foo", "bar"]]
+ Formatted.unregister_parser(:mock)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.