Syntax Highlighting - JavaScript

Previous topic - Next topic

kosson

Dear all,

I have edited a book in the past with Scribus (version 1.3.x), and now I came back to edit a new book dedicated to programming in Javascript.
I really need syntax highlighting for Javascript. I tried with Text filters and it doesn't do the trick. There is no Grep styling and I'm very lost.

I need your guidance and your experience. Are there any of you writing programming books in the community? How do you manage the code excerpts with regards to automatic syntax highlighting?

Thank you all for all the good work and for this amazing piece of software.

GarryP

Welcome to the forum kosson.

Out-of-the-box, Scribus can't do syntax highlighting. The only languages it "knows about" are the human ones as defined in its dictionaries and that's what it concentrates on.

You can define styles for each different bit of syntax but you have to apply them manually as you can't use patterns and/or regular expressions to search for text. Search/replace might be useful - as you can apply styles on the replace function - but there would still be a lot of work involved.

Scribus wasn't built for this kind of thing I'm afraid. I think your best bet might be to use screen-shots from your code editor, just crunch them as much as you can to keep the file size down.

However, there could be someone out there who has created a script that might help. There's no guarantee, but there may be a chance...

kosson

Thank you very much for the info... it's time to panic.

I will experiment with CSS3 Paged Media Module for a while.

I tried also injecting the code excerpts through a frame render  + latex. No avail...

GarryP

You're welcome, although I'm sorry I couldn't give you the info' you need.

Just as a stab in the dark, have you tried http://pygments.org/ ?

Apparently it outputs syntax highlighted code in LaTeX form. I've never used it myself but it might be worth a try.

A quick search shows that there are quite a few LaTeX code highlighters but I have no idea how they work or if they will produce something that can be used in Scribus. Since Scribus uses LaTeX itself to render the text then in theory they should work.

I'm not a LaTeX expert so I can't troubleshoot problems if they come up.

a.l.e

hi kosson,

i've not tried it out yet, but there was a thread in the mailing list on the same topic a few days ago and, if i recall correctly, the suggestion was:

- to use any way that outputs your code formatted as html (an online service that worked for me is http://hilite.me/ )
- copy paste / open the formatted code into libre office writer and save the snippet as an odt file
- load the odt file in a scribus text frame.

i've now tried it out and it worked on a short snippet with scribus 1.5.2.

you can also try to directly load the html file into scribus (i have not tried it).

sadly, for now, you have to do the whole workflow for each frame for each modification of the code that changes the highlighting.

(and, of course, putting the code in a latex render frame will also work!)

GarryP

I tried the above work-flow on Windows with Scribus 1.4.6 and it works if you have the Importer set to "Automatic". Using the "OpenOffice.org Writer Documents" Importer doesn't work for this specific work-flow.

Nermander

Wasn't there a similar topic a few weeks ago where someone used render frames and a LaTeX package for this?

It might have been on the mailing list...

Yes, I found the topic in the mailing list archives:
http://lists.scribus.net/pipermail/scribus/2017-January/053739.html

kosson

#7
@ a.l.e and GarryP -> I've tried, it doesn't work on my setting (Ubuntu 16.10, LibreOffice, Scribus-ng and Scribus nightbuild). It seems I'm condemned to make screenshots, a solution so ugly and messy and time consuming and... ahhhhhh
I will experiment a bit more with latex before I give up and really, really panic.
I have made my peace with Generated Content for Paged Media (CSS3) which leads to some very expensive software packages.

There is a real need here for the programmers. One of the reasons I want to write a beautiful book about Javascript is because there are none. But I'm so stuck right now and I do not want to see what's in the InDesign field for now. I really trust I will sort it out somehow with Scribus.

UPDATE
----
The renderframe is not ready for customized tex files like those using a "plugin" for syntax highlighting. I used minted and it produce beautiful pdf, but it doesn't render in the frame, unfortunately.

\documentclass{article}
\usepackage{minted}
\begin{document}

\begin{minted}{javascript}
console.log(window.trei);
\end{minted}
\end{document}

----
It would be fantastic if the render frame would render html+css. I need to experiment more with odt... maybe?!

a.l.e

if you share the files you have created we can further help you.

anyway:

- the latex way work for sure. if you're comfortable with it, there is a way to get it to work in scribus. but, if you are not sure on how to get it to work in latex, you should first get the snippet to work in latex itself.
- going the html/odt way should probably work. it worked for me (ok, on a mac...). but not all conversion will work. you have to take one that does not use too much css. please provide further details.

finally, it would not be that hard to create some syntax highlighting inside of scribus. i would be ready to work on it. but i'm stuck at understanding how to correctly apply a style from a plugin.

(i have to summarize what i got so far on both plugins i'm working on, and ask on the mailing list for hints... maybe somebody can help me...)

Nermander

#9
Quote from: kosson on February 01, 2017, 11:42:13 AM
The renderframe is not ready for customized tex files like those using a "plugin" for syntax highlighting. I used minted and it produce beautiful pdf, but it doesn't render in the frame, unfortunately.

I thought someone got it working in the mailing list thread, did I remember wrong?

I have a weak memory that it had something to do with that you should not use the full LaTeX preamble... but I'm not 100% sure.

Did you try to get the render frame to work with just normalt LaTex first?


Looks as if the successful attemt was not using minted...
http://lists.scribus.net/pipermail/scribus/2017-January/053768.html


Note that you do NOT need /begin{document} in the render frame.

GarryP

The work-flow on the mailing list - just posted by Nermander - worked for me on Win10 with Scribus 1.4.6 using MiKTeX.

MiKTeX requested that a couple of new packages were to be installed (listings, and something else) but apart from that it works just fine as far as I can tell.

It renders okay on-screen and it shows up as expected in a PDF (1.4, I haven't checked other versions).

a.l.e


-   Generate the highlighted code in html form
    -    Some text editors can do that, otherwise
    -    Paste your code in an online service (like https://tohtml.com/)
    -    Copy the rendered text
-   Paste the highlighted text into a Libreoffice Writer document
-   Save the .odt file
-   Load the .odt file in a text frame in Scribus.

cf. the attached files.


- the latex way has the advantage that you can really modify the code in one place.
- it's simple enough, if you already know latex!

- the .odt way has the advantage that the code is inside of scribus as text and it's easier to make small tweaks.
- it's probably better if you don't know latex (or you're not on linux)

(the code.zip file contains the .odt file, since i cannot upload it directly)

[attachment deleted by admin]

Nermander

I really don't think you need to know much LaTeX for that solution, since once you have a working frame you can copy it and just replace the code (no need to mess with the LaTeX once it is set up). You just wrap the code you want syntax highlighted within two tags.

kosson

#13
@ Nermander and to all

The solution from the email I managed to replicate, though for Javascript there is no joy.

I attach some working file; a code snippet, the tex version with minted plugin and an entire chapter of the book in html + css as pandoc managed to render it for me (ignore the @ page experiments).

This issue of having beautifully rendered code in the pages if proper managed would attract flocks of programmers and finally, we will be in the possession of a tool to print astonishing writings in the age of fast food. We really do need this. Please, coders!

I do thank you very much for helping me cracking this.

P.S.  @ a.l.e: I will experiment further with odt...

[attachment deleted by admin]

kosson

@ a.l.e I managed to do it with the combination of https://tohtml.com/jScript/ and odt.

Beautiful, but it should be part of the Scribus as a core feature.