Skip to content

Commit

Permalink
Started to work on new table helper for zazen. Refs [#228]
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Oct 23, 2008
1 parent b7c65e9 commit a60e834
Show file tree
Hide file tree
Showing 9 changed files with 1,113 additions and 6 deletions.
10 changes: 8 additions & 2 deletions CREDITS
Expand Up @@ -3,6 +3,12 @@ javascript date selection
Mihai Bazon
http://dynarch.com
adapted to Rails by Gaspard Bucher

tablekit table editor
---------------------
Andrew Tetlaw & Millstream Web Software
http://www.millstream.com.au/view/code/tablekit/
add/remove columns and drag&drop support added by Gaspard Bucher

soft icons
----------
Expand All @@ -11,8 +17,8 @@ soft icons

xspf mp3 player
---------------
Fabricio Zuardi
http://musicplayer.sourceforge.net/
Fabricio Zuardi
http://musicplayer.sourceforge.net/

upload progress bar (sample application demo)
-------------------
Expand Down
35 changes: 35 additions & 0 deletions app/helpers/application_helper.rb
@@ -1,5 +1,6 @@
require 'digest/sha1'
require 'tempfile'
require 'json'

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
Expand Down Expand Up @@ -544,6 +545,40 @@ def make_gallery(ids=[], opts={})

render_to_string( :partial=>'nodes/gallery', :locals=>{:gallery=>images} )
end

# Create a table from an attribute
def make_table(opts)
style, node, attribute, title, images, table = opts[:style], opts[:node], opts[:attribute], opts[:title], opts[:images], opts[:table]
case style.sub('.', '')
when ">"
prefix = "<div class='img_right'>"
suffix = "</div>"
when "<"
prefix = "<div class='img_left'>"
suffix = "</div>"
when "="
prefix = "<div class='img_center'>"
suffix = "</div>"
else
prefix = suffix = ""
end

unless table
# get attribute content
attribute = "d_#{attribute}" unless ['v_', 'd_'].include?(attribute[0..1])
text = Node.zafu_attribute(node, attribute)
if text.blank?
table = [{"type"=>"table"},[["title"],["value"]]]
else
table = JSON.parse(text)
end
end
raise JSON::ParserError unless table.kind_of?(Array) && table.size == 2 && table[0].kind_of?(Hash) && table[0]['type'] == 'table' && table[1].kind_of?(Array)

prefix + render_to_string( :partial=>'nodes/table', :locals=>{:table=>table}) + suffix
rescue JSON::ParserError
"<span class='unknownLink'>could not build table from text</span>"
end

def list_nodes(ids=[], opts={})
style = opts[:style] || ''
Expand Down
14 changes: 14 additions & 0 deletions app/views/nodes/_table.rhtml
@@ -0,0 +1,14 @@
<table>
<th>
<% table[1][0].each do |heading| -%>
<td><%= heading %></td>
<% end -%>
</th>
<% table[1][1..-1].each do |row| -%>
<tr>
<% row.each do |td| -%>
<td><%= td %></td>
<% end -%>
</tr>
<% end -%>
</table>
51 changes: 50 additions & 1 deletion lib/parser/lib/rules/zazen.rb
Expand Up @@ -65,14 +65,16 @@ def flush(str=@text)

def scan
#puts "SCAN:[#{@text}]"
if @text =~ /\A([^!"<\n\[]*)/m
if @text =~ /\A([^\|!"<\n\[]*)/m
flush $&
if @text[0..0] == '!'
scan_exclam
elsif @text[0..0] == '"'
scan_quote
elsif @text[0..0] == '['
scan_bracket
elsif @text[0..0] == '|'
scan_pipe
elsif @text[0..4] == '<code'
# FIXME: implement <code..> and @@ instead of "extract"
flush
Expand Down Expand Up @@ -314,6 +316,53 @@ def scan_wiki_link
end
end

def scan_pipe
#puts "PIPE:[#{@text}]"
if @text =~ /\A\|([<=>]\.|)([0-9]+\.|)([a-zA-Z_]+)(\/([^\|]*)|)\|/m
# table |<.34.shopping_list/blah blah|
# table |shopping_list|
#puts "TABLE:#{$~.to_a.inspect}"
eat $&
style, id, attribute, title_opts, title = $1, $2, $3, $4, $5
id = id[0..-2] if id != ''
if @translate_ids
if @translate_ids != :zip
node = @helper.find_node_by_pseudo(id, @context[:node])
id = node.pseudo_id(@context[:node], @translate_ids) if node
end
store "|#{style}#{id}.#{attribute}#{title}|"
else
node = id == '' ? @context[:node] : @helper.find_node_by_pseudo(id, @context[:node])
store @helper.make_table(:style=>style, :node=>node, :attribute=>attribute, :title=>title)
end
elsif @text =~ /\A\|([<=>]\.|)(#{PSEUDO_ID_REGEXP})\.([a-zA-Z_]+)(\/([^\|]*)|)\|/m
# table |<.:art++.shopping_list/blah blah|
# table |shopping_list|
#puts "TABLE SHORTCUT:#{$~.to_a.inspect}"
eat $&
text = $&
style, id, attribute, title_opts, title = $1, $2, $3, $4, $5
if node = @helper.find_node_by_pseudo(id, @context[:node])
if @translate_ids
# replace shortcut
store "|#{style}#{node.pseudo_id(@context[:node], @translate_ids || :zip)}.#{attribute}#{title}|"
else
# write table
store @helper.make_table(:style=>style, :node=>node, :attribute=>attribute, :title=>title)
end
elsif @translate_ids
# node not found, ignore
store text
else
# node not found
store "[#{id} not found]"
end
else
#puts "EAT:[#{$&}]"
# eat marker and continue scan
flush @text[0..0]
end
end

def extract_code(fulltext)
@escaped_code = []
Expand Down
20 changes: 20 additions & 0 deletions lib/parser/test/parser/zazen.yml
Expand Up @@ -116,6 +116,26 @@ image_with_http_link:
image_with_ref:
src: "!http://www.example.org/images/test.jpg!"
res: "<p><img src=\"http://www.example.org/images/test.jpg\" alt=\"\" /></p>"

table:
src: "|shopping_list|"
res: "<p>[make_table attribute:|shopping_list| style:||]</p>"

table_title:
src: "|shopping_list/A list of frequent problems|"
res: "<p>[make_table attribute:|shopping_list| style:|| title:|A list of frequent problems|]</p>"

table_id:
src: "|34.shopping_list|"
res: "<p>[make_table attribute:|shopping_list| node:|34| style:||]</p>"

table_pseudo_id:
src: "|:art+.shopping_list|"
res: "<p>[make_table attribute:|shopping_list| node:|:art+| style:||]</p>"

table_pseudo_path:
src: "|(../some/path).shopping_list|"
res: "<p>[make_table attribute:|shopping_list| node:|(../some/path)| style:||]</p>"

link_with_title:
src: '"this is a title":23'
Expand Down
8 changes: 7 additions & 1 deletion lib/parser/test/parser_test.rb
Expand Up @@ -15,7 +15,7 @@ def blank?

class ParserModule::DummyHelper
def find_node_by_pseudo(*args)
nil
args[0]
end
end

Expand Down Expand Up @@ -74,6 +74,12 @@ def r_test
end
end

class String
def pseudo_id(*args)
self
end
end

class ParserTest < Test::Unit::TestCase
yaml_test :zafu => {}, :zafu_asset => {}, :zafu_insight => {}, :zazen => {} #, :latex => {:module => :zazen, :output => 'latex'}
@@test_parsers = {}
Expand Down

0 comments on commit a60e834

Please sign in to comment.