Search This Blog

Wednesday, February 22, 2017

Drowning

There was a post in the RoboHelp forums about an ExtendScript that will remove inline formatting. I asked the author, Marino Den Baes, if he could share the script and he did.

Let me just say that if I become an "ExtendScript Guru" in the future, it was today, 2/22/2017, when I began that journey.
Object.prototype.isTag = function(Tag) {
//This function check if the token (object) is the specified XML/HTML tag. (Or is one of the specified tags in an array.)
//Attributes are stripped from both the token as the tag to check.
//Works only on tokens from the RoboHelp TokenManager.
var Token = this;

//Check for RoboHelp and check that the object is a token from the RH tokenmanager
if(typeof(RoboHelp) == "undefined")
{
alert("Please start RoboHelp to use the isTag function.");
return false;
}
else if(Token.parent != "[object TokenManager]")
{
alert("You can only use the isTag functions on tokens from the TokenManager.");
return false;
}

if(Token.tokenType != RoboHelp.TokenType.TOKENTAG)//It's not a tag at all
return false;

if(!Tag)//No tag specified and this is a tag so return true
return true;

var isTagCleanTag = function(tag) {
if(tag.match(" "))
tag = tag.split(" ")[0];//Remove attributes the lazy way

return tag.replace(/[<>]/g, "").toLowerCase();
}

var isTag = false;
var fullTag = Token.name;
var TagName = isTagCleanTag(fullTag);//Get the tag name without brackets

if(Tag.constructor.toString().indexOf("Array") == -1)
{
var TagArray = new Array;
TagArray[0] = Tag;
} else {
var TagArray = Tag;
}

for(var i =0; i.)
//Requires isTag function

var Token = this;

if(typeof(isTag) == "undefined")
{
alert("The function balancedDelete requires the function isTag().");
return false;
}
else if(typeof(RoboHelp) == "undefined")
{
alert("Please start RoboHelp to use the balancedDelete function.");
return false;
}
else if(Token.parent != "[object TokenManager]")
{
alert("You can only use the balancedDelete function on tokens from the TokenManager.");
return false;
}

var SingleDelete = function(token) {
var Single = false;

if(token.tokenType == RoboHelp.TokenType.TOKENTEXT)
{
Single = true;
}
else if(token.tokenType == RoboHelp.TokenType.TOKENTAG)
{
if(token.name.match(/(<\/)/g) || token.name.match(/(\/>)/g))//End tags or shorthand tags
Single = true;
else
{
//If no match of yet, make sure that the tag is not a known html shorthand tag. HTML 4.01 allows shorthand tags like

var ShortTags = new Array("area","base","basefont","br","col","frame","img","input","link","meta","param");
for(var i = 0; i
]/g, "").toLowerCase();
}

if(SingleDelete(Token))
Token.delete();
else
{
var TokenDelete = new Array(Token);
var tag = balancedDeleteCleanTag(Token.name);
var endTag = "/"+tag;
var i = 0;
var RetreivedAll = false;
var sToken = Token.next;
while(RetreivedAll == false)
{
if(sToken.isTag(tag))
{
i++;
if(contentdelete)
TokenDelete.push(sToken);
}
else if(sToken.isTag(endTag) && i != 0)
{
i--;
if(contentdelete)
TokenDelete.push(sToken);
}
else if(sToken.isTag(endTag) && i == 0)
{
TokenDelete.push(sToken);
RetreivedAll = true;
}
else if(contentdelete)
TokenDelete.push(sToken);

sToken = sToken.next;
}

for(var j = 0;j
0)
{
var Save = false;
var token = tknmngr.item(1);
while(typeof(token)!='undefined') {

//Style head
if(token.isTag("style"))
{
token.balancedDelete(true);
Save = true;
}

//Inline js
//if(token.isTag("script") && !token.hasAttribute("src"))
//{
//token.balancedDelete(true);
//Save = true
//}

//External js
//if(token.isTag("script") && token.hasAttribute("src"))
//{
//token.balancedDelete(true);
//Save = true;
//}

//Image dimensions
if(token.isTag("img"))
{
token.removeAttribute('width');
token.removeAttribute('height');
Save = true;
}

//Correct URL
var hrefelems = new Array("link", "a");
var srcelems = new Array("script", "img");
if(token.isTag(hrefelems))
{
var url = FixURL(token.getAttribute("href"));
if(url != token.getAttribute("href"))
{
token.setAttribute("href", url);
Save = true;
}
}
else if (token.isTag(srcelems))
{
var url = FixURL(token.getAttribute("src"));
if(url != token.getAttribute("src"))
{
token.setAttribute("src", url);
Save = true;
}
}

//Inline styling tags
var tags = new Array("b","big","center","i","s","small","strike","strong","tt","u");
if(token.isTag(tags))
{
token.balancedDelete(false);
}

//Overrides
if(token.getAttribute("style") != "")
{
if(DHTML(token))
{
var style = "display: none;";
if(token.getAttribute("style") != style)
{
token.setAttribute("style", style);
Save = true;
}
}
else
{
token.removeAttribute("style");
Save = true;
}
}

//Remove empty span -> Housekeeping
if(token.name == "
")
{
token.balancedDelete(false);
Save = true;
}

//Next token
token = token.next;
}
if(Save)//Save file
{
tknmngr.save();
}
//else if(backup)//No save and backup present. Remove backup
//{
//RemoveBackup();
//}

tknmngr.close();

}
//alert('File '+pFile+' processed');
}

function DHTML(token)
{
var CheckPotentialSafeTags = new Array("div", "span");//Potential safe elements
var SafeClasses = new Array("droptext", "expandtext");//Safe classes

var isDHTML = false;

if(token.isTag(CheckPotentialSafeTags))
{
if(in_array(token.getAttribute("class"),SafeClasses))
isDHTML = true;
}
return isDHTML;
}

function Backup()
{
var FileToBackup = new File(file);
BckFileName = file+backupext;
var BckFile = new File(BckFileName);

//If file exists, loop until unique backup filename is created
if(BckFile.exists)
{
var i = 0;
while(BckFile.exists)
{
i++;
BckFile = new File(BckFileName+i);
}
BckFileName = BckFile.fsName;
}
FileToBackup.copy(BckFileName);
}

function RemoveBackup()
{
var BckFile = new File(BckFileName);
BckFile.remove();
}

function FixURL(oldURL) {
var WrongPathDelimiter = /\\/g;
var CorrectPathDelimiter = '/';
var newUrl = oldURL.replace(WrongPathDelimiter, CorrectPathDelimiter);
return newUrl;
}

function in_array(needle,haystack,strict) {
if(!strict)
var strict = false;

var in_array = false;
for(var i=0;i

No comments:

Selling the Farm

Former METALLICA Bassist JASON NEWSTED Selling Montana Ranch For $5 Million