Skip to content

Instantly share code, notes, and snippets.

@lporras
Created May 16, 2013 17:07
Show Gist options
  • Save lporras/5593334 to your computer and use it in GitHub Desktop.
Save lporras/5593334 to your computer and use it in GitHub Desktop.
Methods to transform wordpress snipetts
#gem "acts_as_unvlogable" , git: "git://github.com/michelson/acts_as_unvlogable.git"
String.class_eval do
# Transformer Captions like
# [caption id="attachment_18994" align="alignnone" width="528" caption="perspectiva Roller Contract T 140"]
# CONTENT
# [/caption]
# into
# <div id="attachment_18994" class="wp-caption alignnone" style="width:528px;">
# CONTENT
# <p class='wp-caption-text'>perspectiva Roller Contract T 140</p>
# </div>
#
def to_captions
self.gsub(/\[caption([^\]]*)\]([^\[]*)\[\/caption\]/) do |caption|
attributes = $1
content = $2.strip
div_attributes = {}
attributes.scan(/([^=]*)(=)\"([^\"]*)\"/).each do |attribute|
case attribute[0].strip
when "id"
div_attributes["id"] = attribute[2]
when "width"
div_attributes["width"] = "width: #{attribute[2]}px;"
when "align"
div_attributes["class"] = "wp-caption #{attribute[2]}"
when "caption"
div_attributes["caption"] = attribute[2]
end
end
"<div id='#{div_attributes["id"]}' class='#{div_attributes["class"]}' style='#{div_attributes["width"]}'>#{content.strip}<p class='wp-caption-text'>#{div_attributes["caption"]}</p></div>"
end
# wd_prod.post_content.to_captions
end
# Transforms youtube snippets like
# [youtube width="528" height="350"]http://www.youtube.com/watch?v=tK2fbe1JacY[/youtube]
# into
# <object width='528' height='350'>
# <param name='movie' value='http://www.youtube.com/v/tK2fbe1JacY?version=3&f=videos&app=youtube_gdata[]'></param>
# <param name='allowFullScreen' value='true'></param>
# <param name='allowscriptaccess' value='always'></param>
# <embed src='http://www.youtube.com/v/tK2fbe1JacY?version=3&f=videos&app=youtube_gdata[]' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='528' height='350'></embed>
# </object>
#
def to_embed_youtube
self.gsub(/\[youtube([^\]]*)\]([^\[]*)\[\/youtube\]/) do |youtube|
attributes = $1
url = $2.strip
width = height = nil
attributes.scan(/([^=]*)(=)\"([^\"]*)\"/).each do |attribute|
case attribute[0].strip
when "width"
width = attribute[2]
when "height"
height = attribute[2]
end
end
video = UnvlogIt.new(url)
video.embed_html(width, height)
end
# wd_prod.post_content.to_embed_youtube
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment