Adding wordpress shortcode support
This commit is contained in:
parent
201233f89f
commit
9aa403612e
2
Gemfile
2
Gemfile
|
@ -6,8 +6,10 @@ gem "rails", "4.0.0"
|
|||
#gem "capybara", ">= 1.0.0.beta1"
|
||||
gem "sqlite3"
|
||||
gem "rmagick"
|
||||
gem 'shortcode', :git => 'git@github.com:kernow/shortcode.git'
|
||||
|
||||
group :development, :test do
|
||||
gem 'byebug'
|
||||
gem "rspec-rails", ">= 2.6.0"
|
||||
gem "database_cleaner"
|
||||
gem 'guard-rspec'
|
||||
|
|
181
Gemfile.lock
Normal file
181
Gemfile.lock
Normal file
|
@ -0,0 +1,181 @@
|
|||
GIT
|
||||
remote: git@github.com:kernow/shortcode.git
|
||||
revision: 24516374d7571034aae2817401656cf0af5e6bf7
|
||||
specs:
|
||||
shortcode (0.1.1)
|
||||
haml (~> 4.0)
|
||||
parslet (= 1.5.0)
|
||||
|
||||
PATH
|
||||
remote: ./
|
||||
specs:
|
||||
wordpress-import (0.4.3)
|
||||
bundler (~> 1.0)
|
||||
nokogiri (~> 1.6.0)
|
||||
shortcode (~> 0.1.1)
|
||||
|
||||
GEM
|
||||
remote: http://rubygems.org/
|
||||
specs:
|
||||
actionmailer (4.0.0)
|
||||
actionpack (= 4.0.0)
|
||||
mail (~> 2.5.3)
|
||||
actionpack (4.0.0)
|
||||
activesupport (= 4.0.0)
|
||||
builder (~> 3.1.0)
|
||||
erubis (~> 2.7.0)
|
||||
rack (~> 1.5.2)
|
||||
rack-test (~> 0.6.2)
|
||||
activemodel (4.0.0)
|
||||
activesupport (= 4.0.0)
|
||||
builder (~> 3.1.0)
|
||||
activerecord (4.0.0)
|
||||
activemodel (= 4.0.0)
|
||||
activerecord-deprecated_finders (~> 1.0.2)
|
||||
activesupport (= 4.0.0)
|
||||
arel (~> 4.0.0)
|
||||
activerecord-deprecated_finders (1.0.3)
|
||||
activesupport (4.0.0)
|
||||
i18n (~> 0.6, >= 0.6.4)
|
||||
minitest (~> 4.2)
|
||||
multi_json (~> 1.3)
|
||||
thread_safe (~> 0.1)
|
||||
tzinfo (~> 0.3.37)
|
||||
arel (4.0.2)
|
||||
atomic (1.1.15)
|
||||
blankslate (2.1.2.4)
|
||||
builder (3.1.4)
|
||||
byebug (2.7.0)
|
||||
columnize (~> 0.3)
|
||||
debugger-linecache (~> 1.2)
|
||||
celluloid (0.15.2)
|
||||
timers (~> 1.1.0)
|
||||
celluloid-io (0.15.0)
|
||||
celluloid (>= 0.15.0)
|
||||
nio4r (>= 0.5.0)
|
||||
coderay (1.1.0)
|
||||
columnize (0.3.6)
|
||||
database_cleaner (1.2.0)
|
||||
debugger-linecache (1.2.0)
|
||||
diff-lcs (1.2.5)
|
||||
erubis (2.7.0)
|
||||
fakeweb (1.3.0)
|
||||
ffi (1.9.3)
|
||||
formatador (0.2.4)
|
||||
guard (2.5.1)
|
||||
formatador (>= 0.2.4)
|
||||
listen (~> 2.6)
|
||||
lumberjack (~> 1.0)
|
||||
pry (>= 0.9.12)
|
||||
thor (>= 0.18.1)
|
||||
guard-bundler (2.0.0)
|
||||
bundler (~> 1.0)
|
||||
guard (~> 2.2)
|
||||
guard-rspec (4.2.8)
|
||||
guard (~> 2.1)
|
||||
rspec (>= 2.14, < 4.0)
|
||||
haml (4.0.5)
|
||||
tilt
|
||||
hike (1.2.3)
|
||||
i18n (0.6.9)
|
||||
libnotify (0.8.2)
|
||||
ffi (>= 1.0.11)
|
||||
listen (2.7.0)
|
||||
celluloid (>= 0.15.2)
|
||||
celluloid-io (>= 0.15.0)
|
||||
rb-fsevent (>= 0.9.3)
|
||||
rb-inotify (>= 0.9)
|
||||
lumberjack (1.0.4)
|
||||
mail (2.5.4)
|
||||
mime-types (~> 1.16)
|
||||
treetop (~> 1.4.8)
|
||||
method_source (0.8.2)
|
||||
mime-types (1.25.1)
|
||||
mini_portile (0.5.2)
|
||||
minitest (4.7.5)
|
||||
multi_json (1.9.0)
|
||||
nio4r (1.0.0)
|
||||
nokogiri (1.6.1)
|
||||
mini_portile (~> 0.5.0)
|
||||
parslet (1.5.0)
|
||||
blankslate (~> 2.0)
|
||||
polyglot (0.3.4)
|
||||
pry (0.9.12.6)
|
||||
coderay (~> 1.0)
|
||||
method_source (~> 0.8)
|
||||
slop (~> 3.4)
|
||||
rack (1.5.2)
|
||||
rack-test (0.6.2)
|
||||
rack (>= 1.0)
|
||||
rails (4.0.0)
|
||||
actionmailer (= 4.0.0)
|
||||
actionpack (= 4.0.0)
|
||||
activerecord (= 4.0.0)
|
||||
activesupport (= 4.0.0)
|
||||
bundler (>= 1.3.0, < 2.0)
|
||||
railties (= 4.0.0)
|
||||
sprockets-rails (~> 2.0.0)
|
||||
railties (4.0.0)
|
||||
actionpack (= 4.0.0)
|
||||
activesupport (= 4.0.0)
|
||||
rake (>= 0.8.7)
|
||||
thor (>= 0.18.1, < 2.0)
|
||||
rake (10.1.1)
|
||||
rb-fsevent (0.9.4)
|
||||
rb-inotify (0.9.3)
|
||||
ffi (>= 0.5.0)
|
||||
rmagick (2.13.2)
|
||||
rspec (2.14.1)
|
||||
rspec-core (~> 2.14.0)
|
||||
rspec-expectations (~> 2.14.0)
|
||||
rspec-mocks (~> 2.14.0)
|
||||
rspec-core (2.14.8)
|
||||
rspec-expectations (2.14.5)
|
||||
diff-lcs (>= 1.1.3, < 2.0)
|
||||
rspec-mocks (2.14.6)
|
||||
rspec-rails (2.14.1)
|
||||
actionpack (>= 3.0)
|
||||
activemodel (>= 3.0)
|
||||
activesupport (>= 3.0)
|
||||
railties (>= 3.0)
|
||||
rspec-core (~> 2.14.0)
|
||||
rspec-expectations (~> 2.14.0)
|
||||
rspec-mocks (~> 2.14.0)
|
||||
slop (3.5.0)
|
||||
sprockets (2.12.0)
|
||||
hike (~> 1.2)
|
||||
multi_json (~> 1.0)
|
||||
rack (~> 1.0)
|
||||
tilt (~> 1.1, != 1.3.0)
|
||||
sprockets-rails (2.0.1)
|
||||
actionpack (>= 3.0)
|
||||
activesupport (>= 3.0)
|
||||
sprockets (~> 2.8)
|
||||
sqlite3 (1.3.9)
|
||||
thor (0.18.1)
|
||||
thread_safe (0.2.0)
|
||||
atomic (>= 1.1.7, < 2)
|
||||
tilt (1.4.1)
|
||||
timers (1.1.0)
|
||||
treetop (1.4.15)
|
||||
polyglot
|
||||
polyglot (>= 0.3.1)
|
||||
tzinfo (0.3.39)
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
byebug
|
||||
database_cleaner
|
||||
fakeweb
|
||||
ffi
|
||||
guard-bundler
|
||||
guard-rspec
|
||||
libnotify
|
||||
rails (= 4.0.0)
|
||||
rmagick
|
||||
rspec-rails (>= 2.6.0)
|
||||
shortcode!
|
||||
sqlite3
|
||||
wordpress-import!
|
|
@ -1,5 +1,6 @@
|
|||
require 'nokogiri'
|
||||
require "wordpress/railtie"
|
||||
require 'shortcode'
|
||||
|
||||
module WordPressImport
|
||||
autoload :Author, 'wordpress/author'
|
||||
|
|
|
@ -26,7 +26,7 @@ module WordPressImport
|
|||
end
|
||||
|
||||
def content_formatted
|
||||
formatted = format_syntax_highlighter(format_paragraphs(content))
|
||||
formatted = format_shortcodes(format_syntax_highlighter(format_paragraphs(content)))
|
||||
|
||||
# remove all tags inside <pre> that simple_format created
|
||||
# TODO: replace format_paragraphs with a method, that ignores pre-tags
|
||||
|
@ -103,8 +103,8 @@ module WordPressImport
|
|||
text = ''.html_safe if text.nil?
|
||||
start_tag = tag('p', html_options, true)
|
||||
|
||||
text.gsub!(/\r?\n/, "<br/>\n") # \r\n and \n -> line break
|
||||
text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}") # 2+ newline -> paragraph
|
||||
text.gsub!(/\n\n+/, "</p>#{start_tag}") # 2+ newline -> paragraph
|
||||
text.gsub!(/\r?\n/, "<br/>\n") # \r\n and \n -> line break (must be after the paragraph detection to avoid <br/><br/>)
|
||||
text.insert 0, start_tag
|
||||
|
||||
text.html_safe.safe_concat("</p>")
|
||||
|
@ -121,5 +121,24 @@ module WordPressImport
|
|||
# -> <pre class="brush: ruby">p "Hello world"</pre>
|
||||
text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
|
||||
end
|
||||
|
||||
# Replace Wordpress shortcodes with formatted HTML (see shortcode gem and support/templates folder)
|
||||
def format_shortcodes(text)
|
||||
Shortcode.setup do |config|
|
||||
# the template parser to use
|
||||
config.template_parser = :haml # :erb or :haml supported, :haml is default
|
||||
|
||||
# location of the template files
|
||||
config.template_path = ::File.join(::File.dirname(__FILE__), "..", "..","support/templates/haml")
|
||||
|
||||
# a list of block tags to support e.g. [quote]Hello World[/quote]
|
||||
config.block_tags = [:caption, :column]
|
||||
|
||||
# a list of self closing tags to support e.g. [youtube id="12345"]
|
||||
config.self_closing_tags = [:end_columns, "google-map-v3"]
|
||||
end
|
||||
|
||||
Shortcode.process(text)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
24
support/templates/haml/caption.haml
Normal file
24
support/templates/haml/caption.haml
Normal file
|
@ -0,0 +1,24 @@
|
|||
-# disabling style_hash for now; setting the width = width+10 doesn't seem to actually be a good thing
|
||||
-# style_hash = {:style => "width: #{@attributes[:width].to_i+10}px"} unless @attributes[:width].blank?
|
||||
- figure_hash = {:class => @attributes[:align], :id => @attributes[:id]}
|
||||
-# figure_hash = figure_hash.merge(style_hash) if style_hash
|
||||
- content_image = Nokogiri::HTML(@content).css("img").to_html
|
||||
- content_caption = Nokogiri::HTML(@content).text + " #{@attributes[:caption]}"
|
||||
- @content = "#{content_image} <figcaption>#{content_caption}</figcaption>" unless content_image.blank?
|
||||
%figure{figure_hash}= @content
|
||||
|
||||
-# sample wordpress-y css to go along with this html:
|
||||
figure {
|
||||
background: #f1f1f1;
|
||||
margin-bottom: 20px;
|
||||
padding: 4px;
|
||||
text-align: center;
|
||||
}
|
||||
figure img {
|
||||
margin: 5px 5px 0;
|
||||
}
|
||||
figure figcaption {
|
||||
color: #777;
|
||||
font-size: 12px;
|
||||
margin: 5px 5px 24px;
|
||||
}
|
4
support/templates/haml/column.haml
Normal file
4
support/templates/haml/column.haml
Normal file
|
@ -0,0 +1,4 @@
|
|||
.post_column_1{:style =>"width:#{@attributes[:width]}; float: left; padding: #{@attributes[:padding]} 6% #{@attributes[:padding]} #{@attributes[:padding]}; display: inline;"}= @content
|
||||
|
||||
-#[column width="47%" padding="0"] foo [/column]
|
||||
-# <div class="post_column_1">
|
1
support/templates/haml/end_columns.haml
Normal file
1
support/templates/haml/end_columns.haml
Normal file
|
@ -0,0 +1 @@
|
|||
<div style="clear: both;"></div>
|
37
support/templates/haml/google-map-v3.haml
Normal file
37
support/templates/haml/google-map-v3.haml
Normal file
|
@ -0,0 +1,37 @@
|
|||
- addmarkerlist = @attributes[:addmarkerlist].to_s || ""
|
||||
|
||||
- name = addmarkerlist.split("{}").last
|
||||
- uri_name = URI::encode(name)
|
||||
- uri_address = URI::encode(addmarkerlist.split("{}").first)
|
||||
|
||||
-# google will open up the business page if we pass it a name
|
||||
- uri_address = "#{uri_name},%20#{uri_address}" unless (uri_name == uri_address || name.blank?)
|
||||
|
||||
- gmaps_url = "https://maps.google.com/maps/?q=#{uri_address}&ie=UTF8&t=m"
|
||||
<iframe src="#{gmaps_url}&output=embed" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="#{@attributes[:width]}" height="#{@attributes[:height]}"></iframe>
|
||||
<small>View <a href="#{gmaps_url}">#{name}</a> in a larger map</small>
|
||||
|
||||
|
||||
-# [google-map-v3 width="425"
|
||||
height="350"
|
||||
zoom="12"
|
||||
maptype="roadmap"
|
||||
mapalign="left"
|
||||
directionhint="false"
|
||||
language="default"
|
||||
poweredby="false"
|
||||
maptypecontrol="true"
|
||||
pancontrol="true"
|
||||
zoomcontrol="true"
|
||||
scalecontrol="true"
|
||||
streetviewcontrol="true"
|
||||
scrollwheelcontrol="false"
|
||||
draggable="true"
|
||||
tiltfourtyfive="false"
|
||||
addmarkermashupbubble="false"
|
||||
addmarkermashupbubble="false"
|
||||
addmarkerlist="#1 Dream Manor Dr Globe, AZ{}1-default.png{}Dream Manor Inn"
|
||||
bubbleautopan="true"
|
||||
showbike="false"
|
||||
showtraffic="false"
|
||||
showpanoramio="false"]
|
|
@ -13,6 +13,7 @@ Gem::Specification.new do |s|
|
|||
|
||||
s.add_dependency 'bundler', '~> 1.0'
|
||||
s.add_dependency 'nokogiri', '~> 1.6.0'
|
||||
s.add_dependency 'shortcode', '~> 0.1.1'
|
||||
|
||||
s.add_development_dependency 'rspec-rails'
|
||||
s.add_development_dependency 'database_cleaner'
|
||||
|
|
Loading…
Reference in New Issue
Block a user