Module: MarkdownMedia
- Defined in:
- lib/markdown_media.rb,
lib/markdown_media/version.rb
Constant Summary collapse
- EMBED_REGEX =
/\[\[\s*(http[^\]\s]+(?:\s.+)?)\s*\]\]/
- VERSION =
"2.0.1"
Class Method Summary collapse
- .expanded_embed(url, caption: nil, link: nil, id: nil, type: nil, klass: nil, include_media: true) ⇒ Object
-
.extract_options(url:, include_media: true) ⇒ Object
TODO: there’s got to be a better way, refactor to classes.
- .parse(text, include_media: true) ⇒ Object
- .remove_class(pieces) ⇒ Object
- .remove_id(pieces) ⇒ Object
- .remove_type(pieces) ⇒ Object
- .render_erb(template_slug, locals) ⇒ Object
- .render_markdown(text) ⇒ Object
- .url_or_path?(string) ⇒ Boolean
Class Method Details
.expanded_embed(url, caption: nil, link: nil, id: nil, type: nil, klass: nil, include_media: true) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/markdown_media.rb', line 84 def (url, caption: nil, link: nil, id: nil, type: nil, klass: nil, include_media: true) url = URI.parse(url) = (url: url, include_media: include_media) slug = [:slug] = [:embed_id] type ||= [:type] render_erb slug, { embed_id: || url.to_s, caption: , link: link, id: id, type: type, klass: klass } end |
.extract_options(url:, include_media: true) ⇒ Object
TODO: there’s got to be a better way, refactor to classes
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/markdown_media.rb', line 103 def url:, include_media: true type = nil if url.to_s =~ %r{youtube.com/embed} slug = "youtube" = url.path.split("/embed/")[1] else case url.host when /youtube.com/ slug = "youtube" = nil url.query.split("&").each do |key_value_pair| argument, value = key_value_pair.split("=") if argument == "v" = value end end when "youtu.be" slug = "youtube" = url.path.split("/").map{ |path_piece| path_piece unless path_piece.to_s.empty? }.compact.first when /dailymotion.com/ slug = "dailymotion" = url.path.split("/video/").map{ |path_piece| path_piece unless path_piece.to_s.empty? }.compact.first.split("_").first when "vimeo.com" slug = "vimeo" = url.path.split("/").map{ |path_piece| path_piece unless path_piece.to_s.empty? }.compact.first when "twitter.com" slug = "twitter" type ||= "tweet" when /instagram/ slug = "instagram" when "giphy.com" slug = "giphy" = url.path.split("/").last.split('-').last when "kolektiva.media" slug = "kolektiva" = url.path.split("/").last else slug = case url.path when /\.mp3|\.aac|\.wav|\.ogg|\.oga|\.m4a/ "audio" when /\.mp4|\.avi|\.mov|\.ogv|\.webm|\.m4v|\.3gp|\.m3u8/ "video" when /\.png|\.jpeg|\.jpg|\.gif|\.svg/ "image" else "link" end end end slug = 'link' if include_media == false { slug: slug, embed_id: , type: type } end |
.parse(text, include_media: true) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/markdown_media.rb', line 10 def parse(text, include_media: true) output = text.gsub(EMBED_REGEX) do = $1 unless .to_s.empty? = .split(" ") url = .shift id = remove_id() klass = remove_class() type = remove_type() link = unless .to_s.empty? .pop if url_or_path?(.last) end = .join(" ") (url, caption: , link: link, id: id, type: type, klass: klass, include_media: include_media) end end output end |
.remove_class(pieces) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/markdown_media.rb', line 45 def remove_class(pieces) return if pieces.to_s.empty? klass_string = pieces.detect { |piece| piece =~ /class:\S+/ } if klass_string klass = klass_string.split(":").last pieces.delete(klass_string) klass end end |
.remove_id(pieces) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/markdown_media.rb', line 58 def remove_id(pieces) return if pieces.to_s.empty? id_string = pieces.detect { |piece| piece =~ /id:\S+/ } if id_string id = id_string.split(":").last pieces.delete(id_string) id end end |
.remove_type(pieces) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/markdown_media.rb', line 71 def remove_type(pieces) return if pieces.to_s.empty? type_string = pieces.detect { |piece| piece =~ /type:\S+/ } if type_string type = type_string.split(":").last pieces.delete(type_string) type end end |
.render_erb(template_slug, locals) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/markdown_media.rb', line 170 def render_erb(template_slug, locals) template_path = "/templates/#{template_slug}.erb" template = File.read(File. File.dirname(__FILE__) + template_path) = locals[:embed_id] = locals[:caption] link = locals[:link] id = locals[:id] type = locals[:type] klass = locals[:klass] erb = ERB.new(template) erb.result(binding) end |
.render_markdown(text) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/markdown_media.rb', line 185 def render_markdown(text) ::Kramdown::Document.new( text, input: :kramdown, remove_block_html_tags: false, transliterated_header_ids: true ).to_html end |
.url_or_path?(string) ⇒ Boolean
41 42 43 |
# File 'lib/markdown_media.rb', line 41 def url_or_path?(string) string =~ /^(http|\/)\S+/ ? true : false end |