Created
July 26, 2012 11:56
-
-
Save ttscoff/3181657 to your computer and use it in GitHub Desktop.
Handler for wp_mangler that replaces inline markdown links with references
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def e_sh(str) | |
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/n, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''") | |
end | |
def process_post(input) | |
links = input.scan(/\((https?:\/\/([^\)]+))\)/) | |
refs = input.scan(/^\[([^^][^\]]+)\]: (.*)$/) | |
lines = input.split("\n") | |
bottom = lines[0..-1].join("\n").gsub(/^\[([^^][^\]]+)\]: .*\n?/,'') | |
norepeat = [] | |
norepeatlinks = [] | |
output = [] | |
refs.each {|ref| | |
name = ref[0] | |
next if norepeatlinks.include? ref[1] | |
while norepeat.include? name | |
if name =~ / ?[0-9]$/ | |
name.next! | |
else | |
name = name + " 2" | |
end | |
end | |
output << {'orig' => ref[0], 'title' => name, 'link' => ref[1]} | |
norepeat.push name | |
norepeatlinks.push ref[1] | |
} | |
links.each {|url| | |
next if norepeatlinks.include? url[0] | |
domain = url[0].match(/https?:\/\/([^\/]+)/) | |
parts = domain[1].split('.') | |
name = case parts.length | |
when 1 then parts[0] | |
when 2 then parts[0] | |
else parts[1] | |
end | |
while norepeat.include? name | |
if name =~ / ?[0-9]$/ | |
name.next! | |
else | |
name = name + " 2" | |
end | |
end | |
output << {'orig' => url[0], 'title' => name, 'link' => url[0] } | |
norepeat.push name | |
norepeatlinks.push url[0] | |
} | |
output = output.sort {|a,b| a['title'] <=> b['title']} | |
o = [] | |
output.each_with_index { |x,i| | |
o.push("[#{x['title']}]: #{x['link']}") | |
bottom = bottom.gsub(/\((#{e_sh x['orig']}|#{e_sh x['link']})\)/,"[#{x['title']}]").gsub(/\[#{e_sh x['orig']}\]/,"[#{x['title']}]") | |
} | |
bottom + "\n#{o.join("\n")}\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment