Last active
December 10, 2015 21:38
-
-
Save ocxo/4496236 to your computer and use it in GitHub Desktop.
recursively replace similar lines of html with ssi and remove bogus tags given a nested directory
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # encoding: UTF-8 | |
| #usage: `ruby replace.rb /path/to/target/directory` | |
| require 'iconv' | |
| def read_dir(dir, opts={}) | |
| Dir.glob("#{dir}/*").each_with_object({}) do |f, h| | |
| if File.file?(f) | |
| File.open(f, "r:UTF-8") do |rf| | |
| puts rf.path | |
| if File.extname(rf) =~ /\.php/ || File.extname(rf) =~ /\.html/ | |
| ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') | |
| content = ic.iconv(rf.read) | |
| content.gsub!(/<!-- CSS Stylesheet -->(.*)<title>/m, opts[:inner_header]) | |
| content.gsub!(/<div class="full-width network-bar">(.*)<div class="container body-shadow" id="forumsContainer">/m, opts[:header]) | |
| content.gsub!(/<div class="full-width footer-top">.*<\/html>/m, opts[:footer]) | |
| content.gsub!(/<br \/><div style="z-index:3" class="smallfont" align="center">Search Engine Friendly URLs by <a rel="nofollow" href="http:\/\/www.crawlability.com\/vbseo\/">vBSEO<\/a> 3.1.0<\/div>/m, "") | |
| content.gsub!(/<\?php ="support"; \?>/, "") | |
| content.gsub!(/<\?php ="prodsupport"; \?>/, "") | |
| content.gsub!(/<\?php ="all_old_new"; \?>/, "") | |
| File.open(rf, "w:UTF-8") { |wf| wf.write(content) } | |
| end | |
| end | |
| elsif File.directory?(f) | |
| h[f] = read_dir(f, opts) | |
| end | |
| end | |
| end | |
| inner_header = <<-EOF | |
| <!--#include virtual="/forums/inner-head.inc" --> | |
| <title> | |
| EOF | |
| header = <<-EOF | |
| <!--#include virtual="/forums/header.inc" --> | |
| <div class="container body-shadow" id="forumsContainer"> | |
| EOF | |
| footer = <<-EOF | |
| <!--#include virtual="/forums/footer.inc" --> | |
| EOF | |
| read_dir(ARGV[0], {inner_header: inner_header, header: header, footer: footer}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment