Search This Blog

Showing posts with label ExtendScript. Show all posts
Showing posts with label ExtendScript. Show all posts

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

Tuesday, November 1, 2016

ExtendScript - the New Frontier to Tame

I am using the “link Word documents” functionality within RoboHelp 2015.

My issue is my hyperlink in the Word doc is converted from a relative path to an absolute path.

You see, I have a Word doc in this folder – S:\Dir1\Dir2\Xlec.docx. Inside that file, there is a link to Xlec.pdf. I was trying to make it that a link in the Word doc will open the PDF after I compile my RoboHelp project and distribute browser-based Help. What happens now is the hyperlink is converted to an absolute path and I need it to be a relative path.

The workflow would be
  1. Add a link to the PDF in the Word doc
  2. Link to the Word doc in RoboHelp
  3. The Word document is updated.
  4. I see the little icon that says the Word doc has been updated in RoboHelp.
  5. I copy the PDF to my RH project directory
  6. I recompile my output. When I access the topic that RoboHelp created for the Word doc, the link opens the PDF.

However, what I’ve learned is that when RoboHelp creates the HTML page for the Word doc, it uses an absolute link, not a relative link.

It’s been suggested that a workaround is to write a script in ExtendScript to run whenever I import from Word or update a linked file and go through the topics automatically and change the now absolute link into a relative link.

Currently, I'm on the hunt for an existing ExtendScript that does this. Thus far, I'm not having a lot of success.