forked from magento/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_markdown.rb
39 lines (29 loc) · 1019 Bytes
/
remote_markdown.rb
1
2
3
4
5
6
7
8
9
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
# "THE BEER-WARE LICENSE" (Revision 42):
# <robin.hahling@gw-computing.net> wrote this file. As long as you retain this
# notice you can do whatever you want with this stuff. If we meet some day, and
# you think this stuff is worth it, you can buy me a beer in return.
# Robin Hahling
require 'net/http'
module Jekyll
# Remotely fetch a markdown file.
class RemoteMarkdownTag < Liquid::Tag
def initialize(tag_name, text, tokens)
super
text.strip!
check_protocol(text)
uri = URI(text)
res = Net::HTTP.get_response(uri)
fail 'resource unavailable' unless res.is_a?(Net::HTTPSuccess)
@content = res.body.force_encoding("UTF-8")
end
def render(_context)
@content
end
private
def check_protocol(text)
error_message = "remote_markdown: invalid URI given #{text}"
fail error_message unless text =~ URI.regexp(%w(http https ftp ftps))
end
end
end
Liquid::Template.register_tag('remote_markdown', Jekyll::RemoteMarkdownTag)