Template Commands

Below is a guide to the template commands and examples of how they can be used.

We know templates can appear complex, so if you have any questions or need any help just email us at info@ConvertWordToHtml.com

Need help? We can create your custom templates for you. Just tell us what you need it to do and we can code it. Basic advice is free, for more complex work there may be a small fee. Contact us for details.

List of commands:

ignore_case_on();

Turns on case ignoring when matching (searching for) a text to replace.

 

ignore_case_off();

Turns off case ignoring when matching (searching for) a text to replace.

 

find_and_replace_regexp_text(‘Enter regular expression to find in html file’, ’Enter regular expression to match and replace in text strings that were found by previous expression’, ‘Regular expression or text used to replace any regexp matches from second expression’);

This powerful command can be used to do some regular expression find/replace/remove html processing for a given text that will be found by the first parameter of that command.

For example following command will search input html for a body tag and it would process its manually entered emails (as text) and it would convert it to html versions of it.

find_and_replace_regexp_text(‘<body.*?</body>’,'([\w-]+)@([\w-]+\.)+\w+’,'<a href=”mailto:$0″>$0</a>’);

Input html:

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>EMAIL@TEST.COM</title>

</head>

<body>

<p>

EMAIL@TEST.COM

</p>

</body>

</html>

Output html:

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>EMAIL@TEST.COM</title>

</head>

<body>

<p>

<a href=”mailto:EMAIL@TEST.COM”>EMAIL@TEST.COM</a>

</p>

</body>

</html>

That command only processed body (blue part) of input html and it changed its EMAIL@TEST.COM to html version. If that command would be executed for the whole input html it would also change email that is entered in <title>EMAIL@TEST.COM</title> which would cause html syntax issue.

Example 2:

find_and_replace_regexp_text(‘<p[^<>]*?>.*?</p>’,'([\w-]+)@([\w-]+\.)+\w+’,'<a href=”mailto:$0″>$0</a>’);

Input html:

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>EMAIL@TEST.COM</title>

</head>

<body>

<p>

EMAIL@TEST.COM

</p>

<span>

EMAIL_SPAN@TEST.COM

</span>

<p>

EMAIL2@TEST2.COM

</p>

</body>

</html>

Output html:

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<title>EMAIL@TEST.COM</title>

</head>

<body>

<p>

<a href=”mailto:EMAIL@TEST.COM”>EMAIL@TEST.COM</a>

</p>

<span>

EMAIL_SPAN@TEST.COM

</span>

<p>

<a href=”mailto:EMAIL2@TEST2.COM”>EMAIL2@TEST2.COM</a>

</p>

</body>

</html>

That command was looking for any <p>…</p> tag occurrence in the input html and it changed its EMAIL@TEST.COM to html version. If that command would be executed for the whole input html it would also change email that is entered in <title>EMAIL@TEST.COM</title> which would cause html syntax issue.

Please note that it did not process <span> tag email.

replace_text(‘Text to replace in HTML file’, ’New text’);

Simple command to find and replace any text in html file.

Example – finds all ‘foo1’ strings in html file and replaces it with ‘foo2’:

Command:

replace_text(‘foo1′,’foo2’);

Input html:

<p>foo1 example</p>

<p>foo1foo1foo1</p>

<foo1>foo1</foo1>

Output:

<p>foo2 example</p>

<p>foo2foo2foo2</p>

<foo2>foo2</foo2>

replace_inner_text(‘Text to replace only inside HTML tags(inner text)’, ’New text’);

Simple command to find and replace in inner text in html file.

Example – finds all ‘foo1’ strings in inner text in html file and replaces it with ‘foo2’:

Command:

replace_inner_text(‘foo1′,’foo2’);

Input html:

<p>foo1 example</p>

<p>foo1foo1foo1</p>

<foo1>foo1</foo1>

Output:

<p>foo2 example</p>

<p>foo2foo2foo2</p>

<foo1>foo2</foo1>

replace_regexp_text(‘Enter regular expression to match in html file’, ‘Text used to replace any regexp matches’);

Command to find and replace text in html file. First parameter can contain regular expressions and second one can contain backreferences if there any in first parameter. (For regexp backreferences please look in regular expressions tutorial).

Example 1– finds all <p>…</p> tags in html file and replaces it with ‘foo’ string:

Command:

replace_regexp_text(‘<p>.*?</p>’,’foo’);

Input html:

<p>foo1 example</p>

<p>foo1foo1foo1</p>

<foo1>foo1</foo1>

Output:

foo

foo

<foo1>foo1</foo1>

Example 2– finds all <p>…</p> tags in html file and leaves only inner text of <p> tag:

Command:

replace_regexp_text(‘<p>(.*?)</p>’,’$1′);

Input html:

<p>foo1 example</p>

<p>foo1<p>foo2</p><b>bold</b></p>

<foo1>foo1</foo1>

Output:

foo1 example

foo1<p>foo2<b>bold</b></p> !- this html line is broken because <p></p> tag was embedded in other <p></p> tag and example 2 regexp is mathing <p> and first occurrence of </p> closing tag

<foo1>foo1</foo1>

Example 3– deletes all <p>…</p> tags in html file:

Command:

replace_regexp_text(‘<p[^>/]*>.+?</p>’,”);

Input html:

<p>foo1 example</p>

<p>foo1<p>foo2</p><b>bold</b></p>

<foo1>foo1</foo1>

Output:

!- first line was removed correctly but line below was not all removed because <p> tags were embedded

<b>bold</b></p>

<foo1>foo1</foo1>

replace_tag_attribute(‘HTML tag to replace’, ‘HTML Attribute’, ‘New HTML Attribute’);

Command replaces defined tag attribute with new one in html file.

Example – finds all <p> tags with attribute ‘id=”…”’ in in html file and replaces it with other_attribute=”ABC”:

Command:

replace_tag_attribute(‘p’,’id’,’other_attribute=”ABC”‘);

Input html:

<p id=”123″ bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=”XYZ” >foo1 example</p>

<p>foo1 id=100</p>

Output:

<p other_attribute=”ABC” bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” other_attribute=”ABC” >foo1 example</p>

<p>foo1 id=100</p>

replace_tag_attribute_no_quotes(‘HTML tag to replace’, ‘HTML Attribute’, ’New HTML Attribute’);

Command replaces defined tag attribute with new one in html file. No quotes means that attribute value has no “” (quotes).

Example – finds all <p> tags with attribute ‘id=…’ in in html file and replaces it with other_attribute=”ABC”:

Command:

replace_tag_attribute_no_quotes(‘p’,’id’,’other_attribute=”ABC”‘);

Input html:

<p id=123 bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=”XYZ” >foo1 example</p>

<p>foo1 id=100</p>

Output:

<p other_attribute=”ABC” bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=”XYZ”>foo1 example</p>

<p>foo1 other_attribute=”ABC”</p>

replace_tag_attribute_value(‘HTML tag to replace’, ‘HTML Attribute value to replace’, ’New value’);

Command replaces defined tag attribute value with new value in html file.

Example – finds all <p> tags with attribute ‘id=”…”’ in in html file and replaces it with ABC:

Command:

replace_tag_attribute_value(‘p’,’id’,’ABC’);

Input html:

<p id=”123″ bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=’XYZ’ >foo1 example</p>

<p>foo1 id=100</p>

Output:

<p id=”ABC” bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=’ABC‘ >foo1 example</p>

<p>foo1 id=100</p>

 

replace_tag_attribute_value_no_quotes(‘HTML tag to replace’ , ‘HTML Attribute value to replace’, ‘New value’);

Command replaces defined tag attribute value with new value in html file. No quotes means that attribute value has no “” (quotes).

Example – finds all <p> tags with attribute ‘id=…’ in in html file and replaces it with ABC:

Command:

replace_tag_attribute_value_no_quotes(‘p’,’id’,’ABC’);

Input html:

<p id=123 bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=”XYZ” >foo1 example</p>

<p>foo1 id=100</p>

Output:

<p id=ABC bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=”XYZ” >foo1 example</p>

<p>foo1 id=ABC</p>

replace_inner_text_for_tag(‘HTML tag in which inner text will be replaced’, ‘Text to replace’, ‘New text’);

Command replaces defined tag inner text first occurrence only of parameter string to replace with new string in html file.

Example – finds all <p …> tags with ‘foo’ inside tag and replaces it with XXX:

Command:

replace_inner_text_for_tag(‘p’,’foo’,’XXX’);

Input html:

<p id=”123″>foo1 example</p>

<p bgcolor=”#ff00ff”>xxx foo examplefoo</p>

<p>foo2</p>

Output:

<p id=”123″>XXX1 example</p>

<p bgcolor=”#ff00ff”>xxx XXX examplefoo</p> !- blue underlined foo was not changed because this command matches first occurrence of foo only

<p>XXX2</p>

delete_tag(‘HTML tag to delete in whole file’);

Command deletes defined tag html but leaves its contents in html file.

Example – delete all <p …></p> tags:

Command:

delete_tag(‘p’);

Input html:

<p id=123>foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

Output:

foo1 example

foo1foo2<b>bold</b>

delete_tag_with_content(‘HTML tag to delete in whole file’);

Command deletes defined tag html with its inner contents in html file.

Example – delete all <p …>…</p> tags:

Command:

delete_tag_with_content(‘p’);

Input html:

<p id=123>foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

Output:

!- first line was removed correctly but line below was not all removed because <p> tags were embedded

<b>bold</b></p>

<b>foo</b>

delete_text(‘Text to delete in HTML file’);

Command deletes defined text in html file.

Example – delete all ‘foo‘ strings:

Command:

delete_text(‘foo’);

Input html:

<p id=foo>foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<foo1>foo1</foo1>

Output:

<p id=>1 example</p>

<p bgcolor=”#ffeecc”>1<p>2</p><b>bold</b></p>

<1>1</1>

delete_inner_text(‘Text to delete only inside HTML tags[inner text]’);

Command deletes first occurrence of defined text in inner text of html tag in html file.

Example – delete all ‘foo‘ strings in inner html:

Command:

delete_inner_text(‘foo’);

Input html:

<p id=foo>foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foofoofoo2</p><b>bold</b></p>

<foo1>foo1</foo1>

Output:

<p id=foo>1 example</p>

<p bgcolor=”#ffeecc”>1<p>foofoo2</p><b>bold</b></p> !- foofoo was not removed because it was second and third ocurrence

<foo1>1</foo1>

delete_regexp_text(‘Enter regular expression to delete in html file’);

Command to delete text in html file. Parameter can contain regular expression.

Example 1– deletes all <p>…</p> tags in html file:

Command:

delete_regexp_text(‘<p>.*?</p>’);

Input html:

<p id=foo>foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foofoofoo2</p><b>bold</b></p>

<p>foo1foo1foo1</p>

<foo1>foo1</foo1>

Output:

<p id=foo>foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<b>bold</b></p>

<foo1>foo1</foo1>

delete_inner_text_for_tag(‘HTML tag in which inner text will be replaced’, ‘Text to delete’);

Command deletes second parameter string in defined tag inner text in html file. It deletes first occurrence of second parameter only.

Example – finds all <p …> tags with ‘foo’ inside tag and deletes its fist occurrence:

Command:

delete_inner_text_for_tag(‘p’,’foo’);

Input html:

<p id=”123″>foo1 example</p>

<p bgcolor=”#ff00ff”>xxx foo examplefoo</p>

<p>foo2</p>

Output:

<p id=”123″>1 example</p>

<p bgcolor=”#ff00ff”>xxx examplefoo</p> !- blue underlined foo was not deleted because this command matches first occurrence of foo only

<p>2</p>

delete_all_empty_tags();

Removes all tags that are empty (Empty tag: <tag>any white space inside</tag>).
For example: <p></p> or <p> </p> – will be removed from html.

delete_attribute(‘Attribute to delete in HTML file for every tag’);

Remove defined html tag attribute from all html tags in source html.

Example:

delete_attribute(‘style’);

Input html:

<p class=”test” style=”width:100px”>Test</p>

<span class=”test2” style=”height:100px”>Test2</span>

Output html:

<p class=”test”>Test</p>

<span class=”test2”>Test2</span>

delete_empty_tag(‘HTML tag to delete in whole file’);

Removes defined tag that is empty (Empty tag: <tag>any white space inside</tag>) for example a command to remove all empty ‘p’ tags will be like this:

delete_empty_tag(‘p’);

Will result that all empty tags will be removed from html file

Input html:

<p> </p>

<p>TEST</p>

Output:

<p>TEST</p>

delete_tag_with_content(‘HTML tag to delete in whole file’);

Removes defined tag with its inner content (inner text or html).

Example:

delete_tag_with_content(‘body’);

Will remove body tag with all internal (inner) content it has.

delete_tag_attribute(‘HTML tag in which attribute will be deleted’, ‘Attribute name’);

Command deletes defined tag attribute in html file.

Example – finds all <p> tags with attribute ‘id=”…”’ in in html file and deletes its first occurrence:

Command:

delete_tag_attribute(‘p’,’id’);

Input html:

<p id=”123″ bgcolor=”#ff00ff”>foo1 example</p>

<p id=”123″ bgcolor=”#ff00ff” id=”123″>foo1 example</p>

<p>foo1 id=”100″</p>

Output:

<p bgcolor=”#ff00ff”>foo1 example</p>

<p bgcolor=”#ff00ff” id=”123″>foo1 example</p> !- blue underlined text was not deleted because this command matches first occurrence of foo only

<p>foo1 id=”100″</p>

 

delete_all_tag_attributes(‘HTML tag in which all attributes will be deleted’);

Command deletes all attributes in html file for a given tag.

Example – delete all <p> tag attributes in in html file:

Command:

delete_all_tag_attributes(‘p’);

Input html:

<p id=”123″ bgcolor=”#ff00ff”>foo1 example</p>

<p id=”123″ bgcolor=”#ff00ff” id=”123″>foo1 example</p>

<p>foo1 id=”100″</p>

Output:

<p>foo1 example</p>

<p>foo1 example</p>

<p>foo1 id=”100″</p>

 

add_tag_attribute(‘HTML tag to which attribute will be added’, ‘New attribute with value’);

Command adds defined attribute to a tag in html file.

Example – add to all <p> tags attribute ‘id=”123”’ in html file:

Command:

add_tag_attribute(‘p’,’id=”123”’);

Input html:

<p id=123 >foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

Output:

<p id=123 id=”123″>foo1 example</p>

<p bgcolor=”#ffeecc” id=”123″>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

 

add_html_after_opening_tag(‘HTML opening tag after which HTML will be added’, ‘Text/HTML string’);

Command adds text or html after opening tag in html file.

Example – adds after all <p> tags ‘<b>A Bold Text Example</b>‘ in html file:

Command:

add_html_after_opening_tag(‘p’,'<b>A Bold Text Example</b>’);

Input html:

<p id=123 >foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

Output:

<p id=123><b>A Bold Text Example</b>foo1 example</p>

<p bgcolor=”#ffeecc”><b>A Bold Text Example</b>foo1<p><b>A Bold Text Example</b>foo2</p><b>bold</b></p>

<b>foo</b>

 

add_html_after_closing_tag(‘HTML closing tag after which HTML will be added’, ‘Text/HTML string’);

Command adds text or html after closing tag in html file.

Example – add after all </p> tags ‘<b>A Bold Text Example</b>‘ in html file:

Command:

add_html_after_closing_tag(‘p’,'<b>A Bold Text Example</b>’);

Input html:

<p id=123 >foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

Output:

<p id=123>foo1 example</p><b>A Bold Text Example</b>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>A Bold Text Example</b><b>bold</b></p><b>A Bold Text Example</b>

<b>foo</b>

 

add_html_before_opening_tag(‘HTML opening tag before which HTML will be added’, ‘Text/HTML string’);

Command adds text or html before opening tag in html file.

Example – add before all <p> tags ‘<b>A Bold Text Example</b>‘ in html file:

Command:

add_html_before_opening_tag(‘p’,'<b>A Bold Text Example</b>’);

Input html:

<p id=123 >foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

Output:

<b>A Bold Text Example</b><p id=123>foo1 example</p>

<b>A Bold Text Example</b><p bgcolor=”#ffeecc”>foo1<b>A Bold Text Example</b><p>foo2</p><b>bold</b></p>

<b>foo</b>

 

add_html_before_closing_tag(‘HTML closing tag before which HTML will be added’, ‘Text/HTML string’);

Command adds text or html before closing tag in html file.

Example – add before all </p> tags ‘<b>A Bold Text Example</b>‘ in html file:

Command:

add_html_before_closing_tag(‘p’,'<b>A Bold Text Example</b>’);

Input html:

<p id=123 >foo1 example</p>

<p bgcolor=”#ffeecc”>foo1<p>foo2</p><b>bold</b></p>

<b>foo</b>

Output:

<p id=123>foo1 example<b>A Bold Text Example</b></p>

<p bgcolor=”#ffeecc”>foo1<p>foo2<b>A Bold Text Example</b></p><b>bold</b><b>A Bold Text Example</b></p>

<b>foo</b>