1
0
Fork 0
mirror of https://github.com/fmap/muflax65ngodyewp.onion synced 2024-06-28 10:26:50 +02:00
muflax65ngodyewp.onion/replace_links.rb
2012-10-14 02:12:32 +02:00

39 lines
794 B
Ruby
Executable file

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2012
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
require "backup-urls"
require "awesome_print"
files = Dir["content/references/*.mkd"]
hosts = [
/(wiki\.)?lesswrong/,
]
files.each do |file|
puts "checking #{file} for citations..."
# get all urls
text = File.read(file)
urls = text.scan(/https?:\/\/\S+$/)
# filter unimportant stuff
urls = BackupUrls.filter_urls(urls)
# only backup this stuff
hosts.each{|host| urls.keep_if{|u| u.match host}}
# cite them
cites = BackupUrls.archive urls
# replace urls
cites.each do |url, cite|
text.gsub! %r{#{url}(\s|/)*$}, cite
end
# save changed file
File.open(file, "w") {|f| f.puts text}
end