Scribus Forums

Scribus => Text and Typography => Topic started by: kosson on January 30, 2017, 01:40:41 PM

Title: Syntax Highlighting - JavaScript
Post by: kosson on January 30, 2017, 01:40:41 PM
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.
Title: Re: Syntax Highlighting - JavaScript
Post by: GarryP on January 30, 2017, 03:51:52 PM
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...
Title: Re: Syntax Highlighting - JavaScript
Post by: kosson on January 30, 2017, 04:15:18 PM
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...
Title: Re: Syntax Highlighting - JavaScript
Post by: GarryP on January 30, 2017, 05:11:08 PM
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.
Title: Re: Syntax Highlighting - JavaScript
Post by: a.l.e on January 30, 2017, 05:50:05 PM
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!)
Title: Re: Syntax Highlighting - JavaScript
Post by: GarryP on January 31, 2017, 02:29:43 PM
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.
Title: Re: Syntax Highlighting - JavaScript
Post by: Nermander on January 31, 2017, 05:46:55 PM
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
Title: Re: Syntax Highlighting - JavaScript
Post by: kosson on February 01, 2017, 11:42:13 AM
@ 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?!
Title: Re: Syntax Highlighting - JavaScript
Post by: a.l.e on February 01, 2017, 02:12:26 PM
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...)
Title: Re: Syntax Highlighting - JavaScript
Post by: Nermander on February 01, 2017, 04:41:34 PM
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.
Title: Re: Syntax Highlighting - JavaScript
Post by: GarryP on February 01, 2017, 05:00:49 PM
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).
Title: Re: Syntax Highlighting - JavaScript
Post by: a.l.e on February 01, 2017, 05:24:16 PM

-   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]
Title: Re: Syntax Highlighting - JavaScript
Post by: Nermander on February 01, 2017, 06:05:45 PM
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.
Title: Re: Syntax Highlighting - JavaScript
Post by: kosson on February 02, 2017, 11:25:33 AM
@ 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]
Title: Re: Syntax Highlighting - JavaScript
Post by: kosson on February 02, 2017, 12:53:02 PM
@ 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.

(http://kosson.ro/images/BLOG/NickConstantinescu/Feb2017/SolutionForODT.png)
Title: Re: Syntax Highlighting - JavaScript
Post by: Nermander on February 02, 2017, 04:52:24 PM
Quote from: kosson on February 02, 2017, 11:25:33 AM
The solution from the email I managed to replicate, though for Javascript there is no joy.

Do you mean that it worked with the Java from the example, but not with Javascript?

Then it must be some kind of configuration issue only, because the minted solution relies on Pygments and Pygments supports Javascript.
http://pygments.org/languages/
Title: Re: Syntax Highlighting - JavaScript
Post by: kosson on February 02, 2017, 06:13:17 PM
Indeed, it worked for Java. One big problem though: it rendered the code with every line of code underlined, and in rather poor quality. Hmmmmm... it seems latex plugin doesn't cut it, at least for me.

I will proceed with the LibreOffice Writer imported odt. This path satisfied my requirements. Now concentrating on building a special converter according to two syntax highlighting shema needed in my book.

I will update my thread with my progress. Thank you! If there are any other developments, I receive them with gladness and joy. I just wish someone will pick up this issue and make it work natively for there is a big need to make beautiful materials in the field of programming. Our young must be attracted in a rather dry abstract world by how we pack the info. Many blessings to the coder(s) developing that import plugin preserving color formating.
Title: Re: Syntax Highlighting - JavaScript
Post by: GarryP on February 03, 2017, 10:49:51 AM
I think it would be good if the two techniques mentioned here were put in a Wiki article.

If there are a lot of people doing the same thing it might be useful if the techniques were laid out in a proper work-flow, explaining each step for each, so there is less confusion in the future.