Module: Copywriter::Regex
Instance Method Summary collapse
-
#accepted_name?(filename) ⇒ Boolean
Returns true if this is a file that we will update, (otherwise false).
-
#update_copyright(year, old_content) ⇒ Hash
Updates copyright using regex.
Instance Method Details
#accepted_name?(filename) ⇒ Boolean
Returns true if this is a file that we will update, (otherwise false).
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/github-copywriter/regex.rb', line 17 def accepted_name?(filename) filename = File.basename(filename) names = ["readme", "license"] extensions = [".md", ".txt", ".html"] if names.include? filename.downcase then return true end if extensions.include? File.extname(filename.downcase) then return true end return false end |
#update_copyright(year, old_content) ⇒ Hash
Updates copyright using regex.
Example return hashes:
Input – an out-of-date copyright:
"...Copyright 2013..."
Return:
{:content => "...Copyright 2014...", :copyrights_found => 1, :copyrights_updated => 1}
Input – an already up-to-date copyright:
"...Copyright 2014..."
Return:
{:content => "...Copyright 2014...", :copyrights_found => 1, :copyrights_updated => 0}
Input – no copyright whatsoever:
"...no copyright here..."
Return:
{:content => "...no copyright here...", :copyrights_found => 0, :copyrights_updated => 0}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/github-copywriter/regex.rb', line 52 def update_copyright(year, old_content) data = { :content => "", :copyrights_found => 0, :copyrights_updated => 0 } # Convert the strings to unicode old_content.force_encoding "utf-8" # All teh regex prefix = /(copyright( \(c\)| ©)?|\(c\)|©) /i suffix = /(\.| |$)/ comma_sep = /((\d{4},|\d{4}-\d{4},)*)/ regexs = [ # Singular -> (c) 2012 -> (c) 2014 # for any year { :regex => /#{prefix}\d{4}#{suffix}/, :replace => "\\1 #{year}\\3" }, # Multiple comma separated, ending w/ year_before # (c) 2009-2011,2013 -> (c) 2009-2011,2013-2014 # for year before { :regex => /#{prefix}#{comma_sep}#{year-1}#{suffix}/, :replace => "\\1 \\3#{year-1}-#{year}\\5" }, # Multiple comma separated, ending w/ some_year DASH year_before # (c) 2008-2010,2012-2013 -> (c) 2008-2010,2012-2014 # for year before { :regex => /#{prefix}#{comma_sep}((\d{4}-)?#{year-1})#{suffix}/, :replace => "\\1 \\3\\6#{year}\\7" }, # Multiple comma separated with dash # (c) 2009,2012 -> (c) 2009,2012,2014 # (c) 2009-2012 -> (c) 2009-2012,2014 # for any year { :regex => /#{prefix}#{comma_sep}((\d{4}-)?\d{4})#{suffix}/, :replace => "\\1 \\3\\5,#{year}\\7" }, ] already_updated = /#{prefix}#{comma_sep}(\d{4}-)?#{year}#{suffix}/ has_copyright = /#{prefix}#{comma_sep}(\d{4}-)?\d{4}#{suffix}/ # Loop through each line of the input text old_content.lines.each do |line| # Is there even a copyright? If there isn't, goto the next line. if not (has_copyright === line) then data[:content] << line next end # Are we already updated? If so, goto the next line. if (already_updated === line) then data[:copyrights_found] += 1 data[:content] << line next end # Loop through our regex until we get one to work regexs.each do |r| data[:copyrights_found] += 1 if (r[:regex] === line) updated_line = line.gsub(r[:regex], r[:replace]) # Did the regex update the copyright? If so, goto the next line. if updated_line != line then data[:content] << updated_line data[:copyrights_updated] += 1 break end end end return data end |