-
Notifications
You must be signed in to change notification settings - Fork 4
/
amazon_urls.rb
37 lines (28 loc) · 963 Bytes
/
amazon_urls.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
# A TextFilter that generates Amazon urls
# Usage: [Product Name]( ASIN(10 characters, uppercase letters or numbers) )
# In config.yaml specify: amazon_id (your affiliate id), amazon_base_url (for example: https://amazon.de)
class AmazonUrls < TextFilter
needs :markdown
def render(content)
asin_pattern = /\(\s*([(\d)(A-Z)]{10})\s*\)/
# Set defaults
amazon_base_url = "https://amazon.com"
amazon_id = nil
amazon_base_url = Settings["amazon_base_url"] if Settings["amazon_base_url"]
amazon_id = Settings["amazon_id"] if Settings["amazon_id"]
content.gsub(asin_pattern) do |match|
asin = match[1..10]
amazon_link = "( "
amazon_link += amazon_base_url
amazon_link += "/dp/"
amazon_link += asin
if amazon_id
amazon_link += "?tag="
amazon_link += amazon_id
end
amazon_link += " )"
amazon_link
end
end
end
TextFilterEngine.register_filter(AmazonUrls)