1

I am making a site where code will be displayed as plain text in a PRE tag (no highlighting, comments, no edit tags) and the I want them to be able to click a button and have the highlighted, commented and edit tag code to appear (also in a pre tag) .

Obviously I have to store the highlighted HTML code somewhere but I was looking for a function like a html2text that will take every essence of the edited PRE tag and convert it to plain text with the plain PRE tag

For example something like this

<pre> 
<b>ON SELECT</b> 
{
   "<edit title="This will be the filename.">beep.wav</edit>" SOUND
}
</pre> 

to something like this

<pre> 
ON SELECT 
{
   beep.wav SOUND
}
</pre> 

Edit: also anything that would go in a pre tag that is non-html would'nt have < > tags. There would be > and <'s but not an inclosed < > tag. The scripting language doesnt use it. So that means really all I would need is something to extract any closed < > tags.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
Brian
  • 45
  • 1
  • 5
  • Obligatory: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 even before someone mentions regex (there's always one). – Mark Byers Nov 28 '09 at 03:11
  • Just to note, I've found this PHP function that does HTML to plain text, but it doesnt reconize the spaces, tabs, \n inthe PRE tags. http://www.chuggnutt.com/html2text.php – Brian Nov 28 '09 at 03:18

1 Answers1

2

In Javascript, you could do with DOM functions + regex

<body>
<pre> 
<b>ON SELECT</b> 
{
   "<edit title="This will be the filename.">beep.wav</edit>" SOUND
}
</pre>

<textarea id="result" rows=10 cols=50></textarea>

<script>
    x=document.getElementsByTagName("pre");
    for(i in x){
        text=x[i].innerHTML.replace(/<.*?>/g,'');
        text=text.replace(/"/g,'');
        document.getElementById("result").value+="<pre>"+text+"</pre>";
    }
</script>
</body>

That you will give your expected results, adjust it depends on your need.

<pre> 
ON SELECT 
{
   beep.wav SOUND
}
</pre>

or in PHP, you could use some parsers for HTML

YOU
  • 120,166
  • 34
  • 186
  • 219