Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an import! function

  • Loading branch information...
commit 5886e5cdd6be31f19882efb1143673e9cbf15c56 1 parent fe1b1d6
@rubys authored
View
3  README.md
@@ -282,6 +282,7 @@ HTML methods
* `_svg`: insert svg namespace
* `_math`: insert math namespace
* `_coffeescript`: convert [coffeescript](http://coffeescript.org/) to JS and insert script tag
+* `_import!`: insert markup with indentation matching the current output
* `xhtml?`: output as XHTML?
Note that adding an exclamation mark to the end of the tag name disables this
@@ -291,7 +292,7 @@ Builder extensions
---
* `indented_text!`: matches text indentation to markup
* `indented_data!`: useful for script and styles in HTML syntax
-* `disable_indendation!`: temporarily disable insertion of whitespace
+* `disable_indentation!`: temporarily disable insertion of whitespace
* `margin!`: insert blank lines between tags
Logging:
View
4 demo/wiki.rb
@@ -112,7 +112,7 @@
_input type: 'hidden', name: 'hash',
value: Digest::MD5.hexdigest(@markup)
_div.output do
- _ << markdown.render(@markup)
+ _import! markdown.render(@markup)
end
_div.buttons do
@@ -137,7 +137,7 @@
#display
_div_.content do
- _ << markdown.render(@markup)
+ _import! markdown.render(@markup)
end
_div_.buttons do
View
2  lib/wunderbar/builder.rb
@@ -37,7 +37,7 @@ def indented_data!(data, pre=nil, post=nil, &block)
self << post if post
end
- def disable_indendation!(&block)
+ def disable_indentation!(&block)
indent, level, pending_newline, pending_margin =
indentation_state! [0, 0, @pending_newline, @pending_margin]
text! " "*indent*level
View
63 lib/wunderbar/html-methods.rb
@@ -5,6 +5,13 @@ class HtmlMarkup < Wunderbar::BuilderBase
link meta param source track wbr
)
+ HTML5_BLOCK = %w(
+ # https://developer.mozilla.org/en/HTML/Block-level_elements
+ address article aside audio blockquote br canvas dd div dl fieldset
+ figcaption figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr
+ noscript ol output p pre section table tfoot ul video
+ )
+
def initialize(scope)
@_scope = scope
@x = Wunderbar::XmlMarkup.new :scope => scope, :indent => 2, :target => []
@@ -99,7 +106,7 @@ def method_missing(name, *args, &block)
end
if flag == '!'
- @x.disable_indendation! do
+ @x.disable_indentation! do
@x.tag! name, *args, &block
end
elsif flag == '?'
@@ -193,4 +200,58 @@ def _coffeescript(text)
def clear!
@x.target!.clear
end
+
+ def self.flatten?(children)
+ # do any of the text nodes need special processing to preserve spacing?
+ flatten = false
+ space = true
+ if children.any? {|child| child.text? and !child.text.strip.empty?}
+ children.each do |child|
+ if child.text? or child.element?
+ unless child.text == ''
+ flatten = true if not space and not child.text =~ /\A\s/
+ space = (child.text =~ /\s\Z/)
+ end
+ space = true if child.element? and HTML5_BLOCK.include? child.name
+ end
+ end
+ end
+ flatten
+ end
+
+ def _import!(children)
+ if String === children
+ require 'nokogiri'
+ children = Nokogiri::HTML::fragment(children.to_s).children
+ end
+
+ # remove leading and trailing space
+ children.shift if children.first.text.strip.empty?
+ children.pop if children.last.text.strip.empty?
+
+ children.each do |child|
+ if child.text?
+ text = child.text
+ if text.strip.empty?
+ @x.text! "\n" if text.count("\n")>1
+ elsif @x.indentation_state!.first == 0
+ @x.indented_text! text.gsub(/\s+/, ' ')
+ else
+ @x.indented_text! text.strip
+ end
+ else
+ if self.class.flatten? child.children
+ @x.disable_indentation! do
+ @x.tag!(child.name, child.attributes) {_import! child.children}
+ end
+ elsif child.children.empty?
+ @x.tag!(child.name, child.attributes)
+ elsif child.children.all? {|gchild| gchild.text?}
+ @x.tag!(child.name, child.text.strip, child.attributes)
+ else
+ @x.tag!(child.name, child.attributes) {_import! child.children}
+ end
+ end
+ end
+ end
end
View
2  lib/wunderbar/version.rb
@@ -2,7 +2,7 @@ module Wunderbar
module VERSION #:nodoc:
MAJOR = 0
MINOR = 10
- TINY = 5
+ TINY = 6
STRING = [MAJOR, MINOR, TINY].join('.')
end
View
12 test/test_html_markup.rb
@@ -107,6 +107,18 @@ def test_spaced_collapsed
target
end
+ def test_import_indented
+ @x.html {_div {_import! "<p>one</p><hr><p>two</p>"}}
+ assert_match %r[<div>\n +<p>one</p>\n +<hr/>\n +<p>two</p>\n +</div>],
+ target
+ end
+
+ def test_import_collapsed
+ @x.html {_div {_import! "<p>one, <em>two</em>, three</p>"}}
+ assert_match %r[<div>\n +<p>one, <em>two</em>, three</p>\n +</div>],
+ target
+ end
+
def test_traceback
@x.html {_body? {boom}}
assert_match %r[<pre.*>#&lt;NameError: .*boom],
View
23 tools/web2script.rb
@@ -2,13 +2,7 @@
require 'rubygems'
require 'nokogiri'
require 'optparse'
-
-HTML5_BLOCK = %w(
- # https://developer.mozilla.org/en/HTML/Block-level_elements
- address article aside audio blockquote br canvas dd div dl fieldset
- figcaption figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr
- noscript ol output p pre section table tfoot ul video
-)
+require 'wunderbar'
# Convert a webpage to a Wunderbar script
@@ -116,20 +110,7 @@ def code(element, indent='')
# element has children
elsif element.children.any? {|child| child.element?}
# do any of the text nodes need special processing to preserve spacing?
- flatten = false
- space = true
- if element.children.any? {|child| child.text? and !child.text.strip.empty?}
- element.children.each do |child|
- if child.text? or child.element?
- unless child.text == ''
- flatten = true if not space and not child.text =~ /\A\s/
- space = (child.text =~ /\s\Z/)
- end
- space = true if child.element? and HTML5_BLOCK.include? child.name
- end
- end
- end
- line.sub!(/(\w)( |\.|$)/, '\1!\2') if flatten
+ line.sub! /(\w)( |\.|$)/, '\1!\2' if HtmlMarkup.flatten? element.children
q "#{line} do"
Please sign in to comment.
Something went wrong with that request. Please try again.