<?php################################################################################# ## *****Dreamweaver Comment Stripper***** ## (C)2010 Daniel Sowden. You may use, modify and redistribute this script ## in any way you wish as long as this original notice is maintained and ## credit is given where due... ## Daniel Sowden <daniel3322@gmail.com> ## ################################################################################## ## This is a useful little script I wrote that loops through a directory (and ## its subdirectories) and cleans up the DW specific comments (such as editable ## regions, library includes etc) from files with the extensions html, htm, ## xhtml and xhtm in preparation for uploading to the web. ## ## I couldn't find anything online that specifically does the same thing (yes ## I know there are search and replace apps out there but this does exactly ## what I need it to do) so I am sharing this with all. Enjoy :) ## ## -Dan ## ##################################################################################set up a few things first$extensions = array("html","htm","xhtml","xhtm");$quiet = false;$tidy = false;$output = array();if(!isset($argv[1]) || empty($argv[1]) || $argv[1] == "--help") { echo "****DREAMWEAVER COMMENT STRIPPER****\r\n(C)2010 Daniel Sowden.\r\n"; echo "This script loops through a directory and removes dreamweaver template"; echo "\r\ncomments from all files with the following extensions:\r\n"; foreach($extensions as $extension) echo $extension." "; echo "\r\nUsage: [-qe] ".$argv[0]." <filename>\r\n"; echo "\t-q\tquiet mode\r\n"; echo "\t-t\tuse html tidy on output code (if available)\r\n"; echo "\t-e \tfile extensions to use (optional, comma separated)\r\n"; echo "Examples:\r\n"; echo "\t".$argv[0]." Templates\r\n"; echo "\t\tLoops through the directory \"Templates\" and strips the\r\n"; echo "\t\tcomments from all valid files\r\n"; echo "\t".$argv[0]." -e \"html,xml,dwt\" Templates\r\n"; echo "\t\tLoops through the directory \"Templates\" and strips the\r\n"; echo "\t\tcomments from all files with extensions html, xml and dwt\r\n"; die(); }for($i = 1; $i < $argc; $i++) { if(($argc - $i) == 1) $dirname = $argv[$i]; else { if($argv[$i] == "-q") $quiet = true; elseif($argv[$i] == "-t") $tidy = true; elseif($argv[$i] == "-e" && ++$i <= $argc) $extensions = explode(",",$argv[$i]); else die("Invalid argument \"".$argv[$i]."\"\r\n"); } }if(!isset($dirname)) die("You did not specify a directory\r\n");elseif(!is_dir($dirname)) echo "\"".$dirname."\" is not a valid directory!\r\n";do_recursion($dirname);if(!$quiet) { foreach($output as $out) echo $out; }function do_recursion($directory) { if(is_dir($directory)) { $d = dir($directory); while(($entry = $d->read()) !== false) { echo $debug ? $entry."\r\n" : ""; $currEntry = $directory."/".$entry; if(is_dir($currEntry) && $entry != "." && $entry != "..") do_recursion($currEntry); elseif(correct_type($currEntry)) strip_comments($currEntry); } } }function correct_type($file) { global $extensions; #we need the file extension $ext = explode(".",$file); $count = count($ext); if($count < 2) return false; else $ext = $ext[$count-1]; #ok, go through the list of extnesions: foreach($extensions as $extension) { if($ext == $extension) return true; } return false; }function strip_comments($filename) { global $output, $tidy; $fail = false; if(!is_readable($filename) || !is_writeable($filename)) $fail = "Incorrect perms"; #fail if can't read if(!$fail) { if(!$file = file_get_contents($filename)) $fail = "Can't open"; else { $file = preg_replace("~<!-- InstanceParam.* -->~","",$file); $file = preg_replace("~<!-- InstanceBegin.* -->~","",$file); $file = preg_replace("~<!-- InstanceEnd.* -->~","",$file); $file = preg_replace("~<!-- #Begin.* -->~","",$file); $file = preg_replace("~<!-- #End.* -->~","",$file); if(function_exists("tidy_parse_string") && $tidy) tidy_parse_string($file); if(!file_put_contents($filename,$file)) $fail = "Can't write file"; } } $output[] = "Stripping file ".$filename."...".(!$fail ? "OK" : $fail)."\r\n"; }?>