Add some improvements to kate-syntax-highlighter for use in scripting
ClosedPublic

Authored by xciml on Jul 19 2018, 10:07 PM.

Details

Summary

This patch aims at making the kate-syntax-highlighter tool independent of the filesystem for better use in pipelines.

  • A --stdin option was added to signify that the text stream comes from standard input instead of a file. If a source file is still provided, the program implicitly chooses that instead.

This makes the tool usable in pipelines.

  • MIME types can now be used in the -s option

This is useful for when the file name can't be trusted to give an appropriate type (it might be a hash), but the MIME type is provided by a protocol or database through which the file is accessed. This way translation to the program's syntax names doesn't have to be done outside of the program.

  • A -T/--title option was added to explicitly specify the HTML page's title. I arbitrarily chose "Kate Syntax Highlighter" as a default title from stdin streams and the default is unchanged for a specified source file.

This is needed because now that a filename isn't guaranteed, it's hard to make a default title that's always appropriate. So just let the user specify it without having to rummage through it later to replace it.

Other than these added features, behaviour does (should) not change.

Diff Detail

Repository
R216 Syntax Highlighting
Lint
Lint Skipped
Unit
Unit Tests Skipped
xciml created this revision.Jul 19 2018, 10:07 PM
Restricted Application added projects: Kate, Frameworks. · View Herald TranscriptJul 19 2018, 10:07 PM
Restricted Application added a subscriber: kwrite-devel. · View Herald Transcript
xciml requested review of this revision.Jul 19 2018, 10:07 PM
pino added a subscriber: pino.Aug 12 2018, 4:33 PM
pino added inline comments.
src/lib/htmlhighlighter.h
42–43

Instead of both the variants, what about a single one with QIODevice, e.g.:

void highlightData(QIODevice *dev, const QString &title = QString());

this way you can highlight also an in-memory buffer, for example.

(For the same reason, a setOutputDevice(QIODevice *dev) would be nice too, but it's out of scope for this patch.)

xciml updated this revision to Diff 39700.Aug 14 2018, 2:16 PM

Hi! Good idea, what about this?

I am not sure about the default argument for title, why not just have there QString() and in the code instead of isNull() an isEmpty => use file name fallback.
The definitionForMimeType must perhaps use the priority, too, if there are duplicates, like the definitionForFileName function.
Otherwise: Volker, what do you think?

Otherwise: Volker, what do you think?

Fine with me, the features/changes make sense IMHO.

xciml updated this revision to Diff 40467.Aug 26 2018, 2:31 PM
xciml marked an inline comment as done.

I am not sure about the default argument for title, why not just have there QString() and in the code instead of isNull() an isEmpty => use file name fallback.

I'm not sure what you meant by this, did I get it right? I defaulted to a QStringLiteral in the header to make it clear what the default behaviour would be, but this does make it simpler to work with. I also realised that since user input is taken, the title would have to be HTML escaped, so it works out.

The definitionForMimeType must perhaps use the priority, too, if there are duplicates, like the definitionForFileName function.

I did run into this problem, without checking the priority, the program would return a random match. This should fix it. Thanks for pointing that out!

cullmann accepted this revision.Aug 30 2018, 7:45 AM

Ok, then lets land this, if Volker is ok already ;=)
I will push it, I want to just hide the bestCandidate from the header afterwards.

This revision is now accepted and ready to land.Aug 30 2018, 7:45 AM
This revision was automatically updated to reflect the committed changes.

Thanks for the enhancements!

I just did hide the bestCandidate function as static helper, no need to expose them at all in the header:

Git commit 60affa47908782d2bda94af2e49fa3f1c4608f4e by Christoph Cullmann.
Committed on 30/08/2018 at 07:51.
Pushed by cullmann into branch 'master'.

hide bestCandidate, can be static function inside file

M +12 -12 src/lib/repository.cpp
M +0 -2 src/lib/repository.h

https://commits.kde.org/syntax-highlighting/60affa47908782d2bda94af2e49fa3f1c4608f4e

xciml added a comment.Aug 30 2018, 2:13 PM

Thank you!