0

I want to lean how to highlight syntax of various programming and markup languages. I admit that I have no prior knowledge of this so this might as well be off-topic.

Say, for example, I want to highlight HTML tags. What I think can be done is to use RegEx to find tags.
You can find tags using RegEx and then get their start and end position. Then, you can change the color of that particular text.

However, the above approach will not work if the tags had attributes with them. <img src="...">

So, can someone please guide me as how how this can be achieved.
A third-party API is fine but I would like to learn how this can be done :)

krsteeve
  • 1,794
  • 4
  • 19
  • 29
An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

1

You simply want to highlight HTML code? You can use the SyntaxHighlighter JavaScript library for this.

If you want to learn how, see CSS SyntaxHighlighter 2.0: short tutorial on how to create your own syntax highlighter.

Also, try a simple search for “how to build a syntax highlighter”.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
user1477388
  • 20,790
  • 32
  • 144
  • 264
  • They got rid of the `Android` tag in the edit. How am I supposed to use `SyntaxHighlighter` library in Android (using Java) ? – An SO User Aug 26 '13 at 15:09
  • 1
    Have you tried https://www.google.com/search?q=android+syntax+highlighting+java&oq=android+syntax+highlighting+java&aqs=chrome..69i57j0.9075j0&sourceid=chrome&ie=UTF-8#fp=54b78a9b0d3b5366&q=how+to+create+syntax+highlighting+in+java&spell=1 – user1477388 Aug 26 '13 at 15:44
  • @LittleChild I found this http://stackoverflow.com/questions/1853419/syntax-highlighter-for-java – user1477388 Aug 26 '13 at 15:44
  • I know `JSyntaxPane` that I have used before but it can not be used in Android :( hence asked – An SO User Aug 26 '13 at 16:47
  • @LittleChild Have you seen this http://tex.stackexchange.com/questions/69863/how-to-render-android-listings-with-syntax-highlighting which says, "Using `listings` you can define your own set of rules for syntax highlighting for Android." I'm afraid I am not an android nor Java developer (I do C#/javascript/etc.). – user1477388 Aug 26 '13 at 16:50
  • Oh ok I read what that post says but I am including code *in* a latex document. Rather, I have to let user type the code and I will highlight it. More like an IDE. Sorry for any inconvenience caused. – An SO User Aug 26 '13 at 17:01