Skip to content

Instantly share code, notes, and snippets.

@am3br
Created August 13, 2018 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save am3br/94a2d84d75287c4aa35675911ad5d0ef to your computer and use it in GitHub Desktop.
Save am3br/94a2d84d75287c4aa35675911ad5d0ef to your computer and use it in GitHub Desktop.
Adobe ExtendScript to remove attributes from elements in Robohelp topics
/***********************************************************************************************************
* $$FileName Removeattributes.jsx
*
* $$Description Select topics and remove attributes from html tags, except href,
* src, target and self-closing tags (e.g. img).
*
* This script is intended to clean up topics freshly imported into Adobe Robohelp.
* HOWEVER, it will happily run on your existing topics, and ruin your intended
* styling and RH features.
*
* DISCLAIMER: I'm not a programmer. This has been hacked together from existing
* scripts and my own poor grasp of programming. I don't guarantee the script's
* reliability or security. All I can say is it works for me for my purposes.
*
* I recommend you only have one Robohelp project open when running this script. This
* should prevent some odd error messages.
* The script relies on selecting htm files, so make sure you select the correct ones.
* If you select files from the wrong project, the script will happily run on them.
*
************************************************************************************************************/
var topicmgr;
var udvmgr;
main();
function main()
{
if(typeof(RoboHelp.project)!='undefined')
{
topicmgr = RoboHelp.project.TopicManager;
udvmgr = RoboHelp.project.UDVManager;
getTopics();
}
else
alert('there is no project opened');
}
function getTopics() {
// select the files to process
var listOfFiles = File.openDialog("Select Files to Import", "*.htm", true);
if(listOfFiles!=null)
{
var tFile
for(;listOfFiles.length > 0;)
{
tFile = listOfFiles.pop();
msg("\n\n----Starting process----\n" + tFile.fsName + "\n");
RemoveAttr(tFile.fsName);
}
}
}
function RemoveAttr(filepath)
{
var tokenmgr = RoboHelp.getTokenManager(filepath);
var bSave = false;
var bBodyFound=false;
var bContinue=true;
var bStyleTag = RoboHelp.TagType.TAGSTYLE;
if(typeof(tokenmgr)!='undefined')
{
if(tokenmgr.count>0)
{
var token = tokenmgr.item(1);
while(typeof(token)!='undefined')
{
//don't start processing until we find a body tag
if(!bBodyFound)
{
//we need to see if body tag is there or not
if(token.tokenType == RoboHelp.TokenType.TOKENTAG )
{
if(token.tagType== RoboHelp.TagType.TAGBODY)
{
bBodyFound=true;
//remove attributes on body tag. this is typically link and vlink, which RH html editor doesn't show, even though they exist.
//RH shows a style tag, but Notepad shows the body tag attributes
if(token.attributeCount > 0)
{
var i = token.attributeCount
for ( i; i>0; i-- )
{
var attrName= token.getAttributeName(i);
msg("\n----Removing " + attrName + " from " + token.name);
token.removeAttribute(attrName);
}
}
}
//remove the "OriginalFile" meta tag; I don't need it.
if(token.tagType == RoboHelp.TagType.TAGMETA && token.getAttribute ('name') =='OriginalFile')
{
msg("\n----Removing " + token.name);
token.delete();
bSave = true;
}
//remove any style tag. sometimes the style tag exists, rather than body tag attributes processed above.
if(token.tagType == RoboHelp.TagType.TAGSTYLE)
{
do
{
msg("\n----Removing " + token.name);
token.delete();
token = token.next;
} while (!token.isEndTagofType (bStyleTag));
msg("\n----Removing " + token.name);
token.delete();
bSave = true;
}
}
}
else
{
//check if the token is tag or not
if(token.tokenType == RoboHelp.TokenType.TOKENTAG && bContinue)
// when we find a tag inside the body, if it has attributes delete style, height, width, border, start and type attributes
// doing it this way so href, src, target and self closing tags like img aren't touched.
{
if(token.attributeCount > 0)
{
var i = token.attributeCount
for ( i; i>0; i-- )
{
var attrName= token.getAttributeName(i);
if(attrName == 'style' || attrName == 'height' || attrName == 'width' || attrName == 'border' || attrName == 'start' || attrName == 'type')
{
msg("\n----Removing " + attrName + " from " + token.name);
token.removeAttribute(attrName);
bSave = true;
}
}
}
//remove all span tags that don't have any attributes, just for tidiness
if(token.tagType == RoboHelp.TagType.TAGSPAN && token.attributeCount === 0)
{
msg("\n----Removing " + token.name);
token.delete();
bSave = true;
}
}
}
//we shouldn't need this as we should only process freshly imported word content, not existing topics.
//however, I wasn't sure what this did, so left it in (from the UDV converter script by Adobe)
if(token.tokenType == RoboHelp.TokenType.TOKENXMLPI)
{
//if it is a XMLPI, we need to check if it is UDV or not
if(token.tagType==RoboHelp.PIType.PIUDVSTART)
{
bContinue=false;
}
if(token.tagType==RoboHelp.PIType.PIUDVEND)
{
bContinue=true;
}
}
token = token.next;
}
}
if(bSave)
{
msg('\n\n-----saving file-----\n'+filepath+'\n------end processing topic-----');
tokenmgr.save();
} else {
msg('\n\n----no changes made----');
}
tokenmgr.close();
}
else
alert('there is some error in getting the token manager');
}
function msg(szString)
//write a message to the Robohelp output message pane
{
RoboHelp.project.outputMessage (szString);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment