Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
User defined filters works now :-)
Browse files Browse the repository at this point in the history
Example included.
  • Loading branch information
cth committed May 21, 2008
1 parent 7267355 commit 4679dfd
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 58 deletions.
2 changes: 2 additions & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ app_generators/codex/templates/dp.SyntaxHighlighter/Styles/TestPages.css
app_generators/codex/templates/dp.SyntaxHighlighter/Templates/Test.dwt
app_generators/codex/templates/dp.SyntaxHighlighter/VB.html
app_generators/codex/templates/dp.SyntaxHighlighter/XML.html
app_generators/codex/templates/filters/example_filter.rb
app_generators/codex/templates/html/all.html
app_generators/codex/templates/readme.txt
app_generators/codex/templates/slides/basics.slides
Expand All @@ -61,6 +62,7 @@ app_generators/codex/templates/slides/including_code.slides
app_generators/codex/templates/slides/including_tex.slides
app_generators/codex/templates/slides/metadata.yml
app_generators/codex/templates/slides/table_of_contents.slides
app_generators/codex/templates/slides/user_defined.slides
app_generators/codex/templates/ui/default/blank.gif
app_generators/codex/templates/ui/default/bodybg.gif
app_generators/codex/templates/ui/default/framing.css
Expand Down
4 changes: 2 additions & 2 deletions app_generators/codex/codex_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def manifest
# Create stubs
m.template_copy_each %w[Rakefile]
m.template_copy_each "html/all.html"
%w[basics building example including_code including_tex table_of_contents].each do |slide|
%w[basics building example including_code including_tex table_of_contents user_defined].each do |slide|
m.file_copy_each "slides/#{slide}.slides"
end
m.file_copy_each "slides/metadata.yml"
Expand Down Expand Up @@ -53,7 +53,7 @@ def manifest
"pretty.css", "print.css", "s5-core.css", "slides.css", "slides.js"].each do |asset|
m.file_copy_each "ui/default/#{asset}"
end

m.template_copy_each "filters/example_filter.rb"

m.dependency "install_rubigen_scripts", [destination_root, 'codex'],
:shebang => options[:shebang], :collision => :force
Expand Down
3 changes: 3 additions & 0 deletions app_generators/codex/templates/bin/pressie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
require 'rubygems'
require "codex"

# Load user defined filters
Dir.glob(Dir.pwd + "/filters/*.rb").each { |f| require f }

Codex::Pressie::process
13 changes: 13 additions & 0 deletions app_generators/codex/templates/filters/example_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is _my_ example filter. Define your own ;-)

class TestFilter < Codex::Filter
tag :test

def filter_inline(text,args)
text.reverse
end

def filter_single(args)
args.reverse
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ h1. Creating Slideshows
h1. An Example

* "Some Sample Slides":example.html

h1. Extra filters

* "The Ritex filter":including_tex.html
* "Defining your own":user_defined.html
44 changes: 44 additions & 0 deletions app_generators/codex/templates/slides/user_defined.slides
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
h1. User-defined filters

h1. User-defined filters

* Allow you to create your own @filters@ and @tags@
* They go in the filters/ directory
* There already is an example filter there

h1. Example filter

<div style="width: 50%; float: right">

:code filters/example_filter.rb[class=code-normal]

</div>


<div style="width: 50%">

* Declare a class which inherits from @Codex::Filter@
** Set the tag you wish to use
** Implement @filter_inline(text,args)@
** Implement @filter_single(args)@
** You can omit filter_single if you just want it to load a file and parse it through filter_inline

</div>

It's _that_ simple.

h1. Using your filter

You can now use your tags in your slides:

:inlinecode
:inlinetest
Reverse this text
:endinlinetest
:endinlinecode

The result is:

:inlinetest
Reverse this text
:endinlinetest
3 changes: 2 additions & 1 deletion lib/codex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ module Codex; end

require 'codex/content'
require 'codex/pressie'
require 'codex/filter'
require 'codex/filter'
require 'codex/ritex_filter'
31 changes: 1 addition & 30 deletions lib/codex/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def initialize(original)
def to_html
textile = preprocess_inlinecode(@original)
textile = preprocess_code(textile)
textile = preprocess_inlinetex(textile)
textile = Codex::Filters.instance.filter_all(textile)
content = split_into_slides(textile)
html = RedCloth.new(content).to_html
remove_code_escaping_from(html)
Expand Down Expand Up @@ -108,35 +108,6 @@ def preprocess_inlinecode(text)
result.join("\n")
end

def preprocess_inlinetex(text)
state = :copying
inline_tex = []
result = []
parser = Ritex::Parser.new

text.split(/\n/).each do |line|
case state
when :copying
if line =~ /^:inlinetex/
inline_tex = []
state = :incode
else
result << line
end
when :incode
if line =~ /^:endinlinetex/
result << parser.parse(inline_tex.join("\n"))
parser.flush_macros
state = :copying
else
inline_tex << line
end
end
end

result.join("\n")
end

def split_into_slides(textile)
result = []
slides = textile.split(/^h1/).each do |slide|
Expand Down
55 changes: 31 additions & 24 deletions lib/codex/filter.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# This is an abstract class for user defined filters.
# It lets them declare the tag they wish to use.
require 'singleton'

module Codex
# Filter is an abstract class for user defined filters.
# It lets them declare the tag they wish to use.
# When declaring a tag, it also registers with Filters.
class Filter
attr_accessor :tag
def self.tag(tag)
@tag = tag
(Filters.instance)[tag] = self.new
end
end

# Filters contains a hash of all loaded filters. It does all parsing of the input file
# and send chunks to be filtered to the individual filters.
class Filters
@@instance = nil
attr_accessor :filter_file_tags, :filter_inline_tags, :filter_inline_end_tags

def self.instance
@@instance = self.new if @@instance.nil?
@@instance
end
include Singleton
attr_accessor :filter_file_tags, :filter_inline_tags, :filter_inline_end_tags, :log

def []=(idx,val)
@filters[idx] = val
Expand All @@ -24,26 +26,29 @@ def [](idx)
@filters[idx]
end

# Look for :inlinecode /.../:endinlinecode and substitute in as if it came from a file
# Parse the input text and parse chunks over to filters when encountering tags
def filter_all(text)
state = :copying
result = []
tagged_lines = []
tag = args = nil
text.split(/\n/).each do |line|
case state
when :copying
if line =~ inline_tag
tag = $1
args = $2.strip
state = :inside_tags
state = :inside_tag
elsif line =~ single_tag
result << @filters[tag].filter_single()
tag = $1
args = $2.strip
result << @filters[tag.to_sym].filter_single(args)
else
result << line
end
when :inside_tags
when :inside_tag
if line =~ /^:end/ # :endwhatever or just plain :end
result << @filters[tag].filter_inline(args, tagged_lines)
result << @filters[tag.to_sym].filter_inline(tagged_lines.join("\n"),args)
tagged_lines = []
state = :copying
else
Expand All @@ -54,26 +59,28 @@ def filter_all(text)

result.join("\n")
end


# Creates instance and loads all the user-defined filters in the generated filter/ subdirectory
private
def initialize
@filters = {}
# Load all user filters:
Dir.glob("filters/*.rb").each { |f| require f }
end

# Create regular expression to match single tags (tags with no end tag)
def single_tag
@single_tag_regex = tag_regexp if @single_tag_regex.nil?
@single_tag_regex
end

# Create regular expression to match inline tags. By convention these are all prefixed "inline"
def inline_tag
@inline_tag_regex = tag_regexp("inline") if @inline_tag_regex.nil?
@inline_tag_regex
end

def single_tag
@single_tag_regex = tag_regexp if @single_tag_regex.nil?
@single_tag_regex
end


# Does the actually work of creating matcher regular expressions
def tag_regexp(prefix = "")
Regexp.new("^:#{prefix}(" + (@filters.map { |idx,filter| filter.tag.to_s }).join('|') + ")(.*)")
Regexp.new("^:#{prefix}(" + (@filters.map { |tag,filter| tag.to_s }).join('|') + ")(.*)")
end
end
end
2 changes: 2 additions & 0 deletions lib/codex/ritex_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ class RitexFilter < Codex::Filter
tag :webtex

def filter_single(text,args)
puts "filter single"
Ritex::Parser.new.parse(text)
end

def filter_inline(text,args)
puts "filter inline"
Ritex::Parser.new.parse(text)
end
end
2 changes: 1 addition & 1 deletion test/test_codex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def setup
def test_truth
assert true
end
end
end
14 changes: 14 additions & 0 deletions test/test_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require File.dirname(__FILE__) + '/test_helper.rb'

class TestFilters < Test::Unit::TestCase
def setup
end

def teardown
end

def test_truth
assert true
end

end

0 comments on commit 4679dfd

Please sign in to comment.