Highlight.js – Syntax highlighting for the Web
highlightjs.org
highlightjs.org
http://highlightjs.readthedocs.org/en/latest/line-numbers.ht...
I like Prism so far and don't have any issues with it yet
Clutter and simplicity is not an emotional argument. It becomes verifiably more difficult to maintain a code base with each new line. And since he's the one doing the maintaining, his position seems quite reasonable.
Secondly, he seems to have a strong aesthetic aversion to line numbers, and I can totally respect that. Frankly, among popular open source projects, those whose developers have the strongest sense of style and design and usually (not always) the best.
As such, verbal cues (or a long stick) are preferable to a laser pointer. Of course, use a laser pointer to assist those who can see it, but it's better assume half the people in the room can't and use words to this effect.
If I am copying and pasting code from a website, it usually goes to my editor and I don't want to have to delete the line numbers from it.
https://groups.google.com/d/msg/highlightjs/UVJaQcQNC1c/1C6U...
The approach this library takes to building the highlighted DOM tree is very clever. It represents the original DOM and the highlighted code output as separate streams of tag open / close events and then merges them together. This allows the highlighter to maintain the pre-existing DOM structure of the highlighted code! The language auto-detection and sub-language handling are also very neat.
If you like interesting codebases, I'd recommend giving it a quick read. :)
Here is where you extract the existing HTML tags: https://github.com/ioquatix/jquery-syntax/blob/97e38d08924d7...
When you brush the code (extract highlighing information) you provide the set of initial matches which are converted into a tree: https://github.com/ioquatix/jquery-syntax/blob/97e38d08924d7... - there is no merge step, they are merged in place in the tree which is then used to generate DOM (or whatever you like really) output.
I never implemented language auto-detection but I'm sure I've seen that implemented before prism before, perhaps Google's Prettify might have been one of the first to do it. It basically highlights using all available brushes and looks at which one matches the most tokens - not sure if this is how it is currently implemented though. My feeling is that you usually know what language you are highlighting, the auto-highlighter wouldn't always get it right, and the cost of loading all brushes/languages is pretty high (imagine for example you have 100 different languages supported, you'd have to do all of them)..
The sub-language handling in jQuery.Syntax is as precise as possible: https://github.com/ioquatix/jquery-syntax/blob/97e38d08924d7...
Basically, if you match some sub-portion of the code, you then highlight that using a different brush. Sometimes it's almost impossible to know though (e.g. diffs which may be incomplete).
The highlighting (lexing) could (and should) be done ONCE at the server. Imagine some high traffic blogs with hundreds of millions of hits combined - all these clients downloading the highlighting libs + lexers for 50 different languages that will never be used on the blog anyway. The wasted energy here is probably even measurable.
Cons when letting the clients handle syntax highlighting:
- Much slower load times
- Wasted energy
- Can fail because of JS crashing
- Noscript users won't get any highlighting (and your blog targets technical users, right? They are more likely to use no script)
- Slow mobile devices will have a harder time loading the site (or depending on javascript engine - failing highlight)
- (if no site optimization is made) the client will download lexers for languages not used on the blog
Cons when the server do the lexing ONCE:
- NOTHING
Note that this rant isn't targeted at Highlight.js. It's a good highlighter - I use it myself at my blog. Except I use it on the server via a CGI script. It took me no more than 2 minutes to set up.
How about you write a tutorial about this and submit it to HN? This is probably a better way to make people adapt your approach than a rant, no matter how valid your points are :)
This is a battle you cannot win and should not attempt to fight.
Cons when the server do the lexing ONCE:
- NOTHING
You are assuming a specific use case, where static code originally exists on the server and the highlighted version is included in a page that is viewed many times.My use case for these libraries is different: users actually write 'code' in my app, which I wish to highlight as they type it. Of course many users will write the same fragments and energy could be saved by caching the highlighting of those on the server. However, the latency of that solution (roundtrip to server needed to highlight code) and the code complexity (cache expiration is one of the two 'hard' problems in SE) are much worse than for the client-side highlighting solution.
My rantiness (I apologize for that) targets programming blogs/static pages.
These syntax highlighters are great but I think that underlie the lack of a defined and accessible AST parse definition for most languages. Highlight.js and others kind of just rely regexes-- https://github.com/isagalaev/highlight.js/blob/master/src/la...
It'd be great if we could parse through programming languages to get their meaning. I want to get tuples back!
``` a = 23 ``` would give
(variable(name=a), assignedTo, number(23))
This does exist for some languages, but mostly compiled ones. But it would make the syntax highlighting even more robust! Antlr is the best one around at the moment. http://www.antlr.org/
I experimented editing code using jQuery.Syntax. I used the match tree it generated to figure out where to restart parsing and it was pretty fast, it would only re-evaluate the current line in most cases.
A lot of events record the presenters screen and the presenter but expect the presenter to stand in one spot so they don't need a cameraman.
So, it does not work very well to point at a screen when presenting.
* I'm the author so of course I'd say that :)
Basically I want to be able to annotate groups of 1 or more lines, something like this, probably using some kind of inline comment
http://reference.bitreactive.com/reference/images/tutorial/s...
I'm not talking a full-on programming editor, that's a much larger scope. Rather, I have an admin tool with a <textarea> that allows entering short code snippets, and it would be handy to see highlighting as you type. Even at this low level of complexity, is it simpler to just embed something like Ace, or is there an easy way to use a highlighting library?
edit: I should mention that this might be a cool example for someone looking to use hljs in React/Flux-- I tried to make it fairly clean. There's a github link on the bottom-left of the website.
Unfortunately, the tradeoff is no support for generating a display with line numbers. In my case, that was the lesser of two evils, but I wouldn't mind using two libraries if it meant I could get everything I want.
If you have Ruby in your stack, it's easy -- use github's own https://github.com/github/linguist
You might also be able to use just the language definition files from https://github.com/syntaxhighlighter
Planning to open source an http API (similar to http://markup.su/highlighter).
https://www.dougcodes.com/go-lang/building-a-web-application...
Dead easy to implement, just set it and forget it.
• SyntaxHighlighter
generates tons of nested tables, doesn’t work well with Bootstrap (overflows into right rail)
• Google Prettify
Worked okay, but kind of ugly. Had to use jQuery to apply “prettyprint” class to all pre elements.
• Highlight.js
Easy to set up, lots of themes, seems pretty quick, regular updates
Sample post: http://weblogs.asp.net/jongalloway/looking-at-asp-net-mvc-5-...
Highlight.js + styles are hosted on cdnjs, which makes it easy to host on a blog.