| 1 |
#!/usr/bin/perl -pli.bak |
| 2 |
|
| 3 |
# Copyright (c) 2006, Google Inc. |
| 4 |
# All rights reserved. |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are |
| 8 |
# met: |
| 9 |
# |
| 10 |
# * Redistributions of source code must retain the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer. |
| 12 |
# * Redistributions in binary form must reproduce the above |
| 13 |
# copyright notice, this list of conditions and the following disclaimer |
| 14 |
# in the documentation and/or other materials provided with the |
| 15 |
# distribution. |
| 16 |
# * Neither the name of Google Inc. nor the names of its |
| 17 |
# contributors may be used to endorse or promote products derived from |
| 18 |
# this software without specific prior written permission. |
| 19 |
# |
| 20 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
# |
| 32 |
# --- |
| 33 |
# Author: Craig Silverstein |
| 34 |
|
| 35 |
# This is a perl script for editing your source files that were |
| 36 |
# written with ctemplate <0.95, to use the new #include paths, |
| 37 |
# namespace names, and function locations in ctemplate 0.95. |
| 38 |
# |
| 39 |
# This script will modify your files in place. It is safe to |
| 40 |
# call it multiple times. To call it, do something like: |
| 41 |
# find <root of my project tree> -print0 | xargs -0 ./convert_to_95.pl |
| 42 |
# |
| 43 |
# Issues: |
| 44 |
# TemplateFromString logic is tricky. Best to check up by hand. |
| 45 |
# Can't always tell when 1st arg to TemplateFromString is empty string. |
| 46 |
# If it might be for your app, change the logic to use StringToTemplate. |
| 47 |
# Fails if TemplateFromString or StringToTemplate span multiple lines. |
| 48 |
|
| 49 |
# Change all the #include lines |
| 50 |
s@google/template.h@ctemplate/template.h@g; |
| 51 |
s@google/per_expand_data.h@ctemplate/per_expand_data.h@g; |
| 52 |
s@google/template.h@ctemplate/template.h@g; |
| 53 |
s@google/template_annotator.h@ctemplate/template_annotator.h@g; |
| 54 |
s@google/template_dictionary.h@ctemplate/template_dictionary.h@g; |
| 55 |
s@google/template_dictionary_interface.h@ctemplate/template_dictionary_interface.h@g; |
| 56 |
s@google/template_emitter.h@ctemplate/template_emitter.h@g; |
| 57 |
s@google/template_enums.h@ctemplate/template_enums.h@g; |
| 58 |
s@google/template_from_string.h@ctemplate/template_from_string.h@g; |
| 59 |
s@google/template_modifiers.h@ctemplate/template_modifiers.h@g; |
| 60 |
s@google/template_namelist.h@ctemplate/template_namelist.h@g; |
| 61 |
s@google/template_pathops.h@ctemplate/template_pathops.h@g; |
| 62 |
s@google/template_string.h@ctemplate/template_string.h@g; |
| 63 |
|
| 64 |
# Change all the namespace uses |
| 65 |
s@namespace\s+google@namespace ctemplate@g; # brought into google namespace |
| 66 |
s@\bgoogle::@ctemplate::@g; |
| 67 |
s@namespace\s+template_modifiers\s*{@@ && ($in_tm = 1); |
| 68 |
$in_tm && s@}@@ && ($in_tm = 0); |
| 69 |
s@\btemplate_modifiers::@@g; |
| 70 |
|
| 71 |
# Change TemplateFromString::GetTemplate to |
| 72 |
# Template::StringToTemplateCache(cache_key, template_text); |
| 73 |
# return Template::GetTemplate(cache_key, strip); |
| 74 |
# unless cache_key is the empty string, in which case it should be |
| 75 |
# return Template::StringToTemplate(template_text, strip, TC_MANUAL); |
| 76 |
# We have to be tricky, to do this in one command. |
| 77 |
# Better would be to just do Template::StringToTemplate(), but then we |
| 78 |
# have to free the memory when we're done, which is too hard to do here. |
| 79 |
s@TemplateFromString::GetTemplate\s*\(""\s*,([^,]+),([^,]+)\)@Template::StringToTemplate($1, $2, ctemplate::TC_MANUAL)@; |
| 80 |
s@TemplateFromString::GetTemplate\s*\(([^,]+),([^,]+),([^,]+)\)@Template::GetTemplate((ctemplate::Template::StringToTemplateCache($1, $2), $1), $3)@; |
| 81 |
|
| 82 |
if (m@TemplateFromString::GetTemplate\s*\([^)]*$@) { |
| 83 |
warn("$ARGV:$.: TemplateFromString spans mutiple lines. You will need " . |
| 84 |
"to clean it up by hand. See comments in $0."); |
| 85 |
} |
| 86 |
|
| 87 |
# Get rid of the 4th arg to Template::TemplateToString. If it's |
| 88 |
# TC_MANUAL, that's fine. If it's not, we need to tell the user to |
| 89 |
# fix this up manually by adding an {{%PRAGMA AUTOESCAPE}} to the |
| 90 |
# beginning of their string. |
| 91 |
if (s@(\bStringToTemplate\s*\([^)]*),\s*TC_([A-Z_]*)@$1@g) { |
| 92 |
if ($2 ne "MANUAL") { # TC_MANUAL is the new default |
| 93 |
warn("WARNING: $ARGV:$.: You will need to manually fix up the " . |
| 94 |
"StringToTemplate change to add an AUTOESCAPE {{\%pragma}} to " . |
| 95 |
"the beginning of the input string, to set the context to TC_$2\n"); |
| 96 |
} |
| 97 |
} |
| 98 |
if (m@\bStringToTemplate\s*\([^)]*$@) { |
| 99 |
warn("$ARGV:$.: StringToTemplate spans mutiple lines. You will need " . |
| 100 |
"to clean it up by hand: get rid of the TC_foo arg. If it's not " . |
| 101 |
"TC_MANUAL, you will need to add an AUTOESCAPE pragma. " . |
| 102 |
"See comments in $0."); |
| 103 |
} |
| 104 |
|
| 105 |
# Change all the TemplateDictionary escapes. Now they are globals in |
| 106 |
# the ctemplate namespace. |
| 107 |
s@TemplateDictionary::html_escape@html_escape@g; |
| 108 |
s@TemplateDictionary::pre_escape@pre_escape@g; |
| 109 |
s@TemplateDictionary::xml_escape@xml_escape@g; |
| 110 |
s@TemplateDictionary::javascript_escape@javascript_escape@g; |
| 111 |
s@TemplateDictionary::url_query_escape@url_query_escape@g; |
| 112 |
s@TemplateDictionary::json_escape@json_escape@g; |
| 113 |
|
| 114 |
# This cleans up anything messy we left behind. |
| 115 |
s@\bctemplate::ctemplate::@ctemplate::@g; # get rid of redundant specifier |
| 116 |
|
| 117 |
close ARGV if eof; # to reset $. for each file |