Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static highlighting #11

Closed
pietroppeter opened this issue Mar 8, 2021 · 11 comments
Closed

static highlighting #11

pietroppeter opened this issue Mar 8, 2021 · 11 comments
Labels
enhancement New feature or request

Comments

@pietroppeter
Copy link
Owner

pietroppeter commented Mar 8, 2021

there's no really need to add javascript to highlight stuff at runtime. I should be able to do it when rendering html.
there was some discussion today in IRC #science channel (which unfortunately is not logged).

@ghost
Copy link

ghost commented Mar 8, 2021

Some options:

There are many other options of course

@Vindaar
Copy link
Contributor

Vindaar commented Mar 8, 2021

Just adding Pygments, because aside from the Python dependency it makes for a good alternative:

https://pygments.org/

Mini example

Wouldn't use it as a default to not have it as a dependency, but it might be nice as an alternative. I use it when exporting Org files to LaTeX and HTML. I like it, because of the large number styles it ships with and in my experience it just works.

I've never used it as a library though, so I don't know how easy it is to interface with from that point of view.

@pietroppeter pietroppeter added the enhancement New feature or request label Mar 8, 2021
@ghost
Copy link

ghost commented Mar 8, 2021

@Vindaar the chroma tool I linked actually uses pygments definitions under the hood - "Chroma is based heavily on Pygments, and includes translators for Pygments lexers and styles."

@Vindaar
Copy link
Contributor

Vindaar commented Mar 8, 2021

@Vindaar the chroma tool I linked actually uses pygments definitions under the hood - "Chroma is based heavily on Pygments, and includes translators for Pygments lexers and styles."

Oh, haha. I thought it was a pure Go solution.

@ghost
Copy link

ghost commented Mar 8, 2021

@Vindaar the chroma tool I linked actually uses pygments definitions under the hood - "Chroma is based heavily on Pygments, and includes translators for Pygments lexers and styles."

Oh, haha. I thought it was a pure Go solution.

nonono, you're right in that it's a pure Go solution, but it uses existing Pygments definitions to not reinvent the wheel

@ghost
Copy link

ghost commented Mar 8, 2021

A rough prototype of using Nim's highlite module together with highlight.js's own syntax highlighting styles to make static highlighting - https://gist.github.com/Yardanico/144caea455c43917a409a22b56f34c03
to run correctly don't forget to do:

nbDoc.context["no_default_highlight"] = true
nbDoc.partials["style"] = """
<link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/pietroppeter/nimib/assets/atom-one-light.css'>
"""

after nbInit

@ghost
Copy link

ghost commented Mar 8, 2021

@pietroppeter when you'll implement your idea with splitting the rendering backends I'll add this highlighting myself, or you can add it yourself earlier if you want :)

@ghost
Copy link

ghost commented Mar 8, 2021

Fixed escaping for my code, now it can highlight itself (more or less) correctly:
image
image

@pietroppeter
Copy link
Owner Author

hey @Yardanico, thanks for this it looks very nice and exactly in the direction I wanted it to go! Also, I think the render refactoring is orthogonal to this change, I can do it later. If you want to open a PR I would be very happy and we can take it from there!

As for code structure, you could add a highlight module in src\nimib with and exportable highlightNim function that will be used in renderHtmlCodeBody exactly how you do now. As a further example to test you can use the Nim example in highlight.hs/demo.

@pietroppeter
Copy link
Owner Author

fixed by #14 (additional commits in #15). In the end the only thing that got lost from removing highlight.js is the highlighting the pragmas which is not straightforward to do with the pure nim highlite approach so the tradeoff is still very much on the positive side (I also do not currenlty have any pragmas in the example code). Thanks a lot @Yardanico!

@pietroppeter
Copy link
Owner Author

putting here for future reference work by @Yardanico reported in #science channel (not logged) of how one could improve nim highlite to somewhat parse pragmas:

diff --git a/lib/packages/docutils/highlite.nim b/lib/packages/docutils/highlite.nim
index 8c91e0a8e..de82719c4 100644
--- a/lib/packages/docutils/highlite.nim
+++ b/lib/packages/docutils/highlite.nim
@@ -339,7 +339,24 @@ proc nimNextToken(g: var GeneralTokenizer, keywords: openArray[string] = @[]) =
             g.state = g.kind
             break
           else: inc(pos)
-    of '(', ')', '[', ']', '{', '}', '`', ':', ',', ';':
+    of '{':
+      inc(pos)
+      # pragma opening?
+      if g.buf[pos] == '.':
+        inc(pos)
+        g.kind = gtDirective
+      else:
+        g.kind = gtPunctuation
+    of '.':
+      inc(pos)
+      # possible pragma closing
+      if g.buf[pos] == '}':
+        inc(pos)
+        g.kind = gtDirective
+      else:
+        g.kind = gtOperator
+        while g.buf[pos] in OpChars: inc(pos)
+    of '(', ')', '[', ']', '}', '`', ':', ',', ';':
       inc(pos)
       g.kind = gtPunctuation
     of '\0':

doesn't handle whitespace in pragmas ( like { .) nor pragmas without . at the end
if a pragma is defined as { . test . } it won't work
because highlite is really just a hand-written lexer for simple highlighting (it doesn't use regexes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants