1 | /* |
2 | SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> |
3 | |
4 | SPDX-License-Identifier: MIT |
5 | */ |
6 | |
7 | #include "ksyntaxhighlighting_version.h" |
8 | |
9 | #include <KSyntaxHighlighting/Definition> |
10 | #include <KSyntaxHighlighting/DefinitionDownloader> |
11 | #include <KSyntaxHighlighting/Repository> |
12 | #include <KSyntaxHighlighting/Theme> |
13 | #include <ansihighlighter.h> |
14 | #include <htmlhighlighter.h> |
15 | |
16 | #include <QCommandLineParser> |
17 | #include <QCoreApplication> |
18 | #include <QFile> |
19 | |
20 | #include <cstdio> |
21 | |
22 | using namespace KSyntaxHighlighting; |
23 | |
24 | template<class Highlighter, class... Ts> |
25 | static void applyHighlighter(Highlighter &highlighter, |
26 | QCommandLineParser &parser, |
27 | bool fromFileName, |
28 | const QString &inFileName, |
29 | const QCommandLineOption &outputName, |
30 | const Ts &...highlightParams) |
31 | { |
32 | if (parser.isSet(option: outputName)) { |
33 | highlighter.setOutputFile(parser.value(option: outputName)); |
34 | } else { |
35 | highlighter.setOutputFile(stdout); |
36 | } |
37 | |
38 | if (fromFileName) { |
39 | highlighter.highlightFile(inFileName, highlightParams...); |
40 | } else { |
41 | QFile inFile; |
42 | if (inFile.open(stdin, ioFlags: QIODevice::ReadOnly)) { |
43 | highlighter.highlightData(&inFile, highlightParams...); |
44 | } |
45 | } |
46 | } |
47 | |
48 | static Theme theme(const Repository &repo, const QString &themeName, Repository::DefaultTheme t) |
49 | { |
50 | if (themeName.isEmpty()) { |
51 | return repo.defaultTheme(t); |
52 | } |
53 | return repo.theme(themeName); |
54 | } |
55 | |
56 | int main(int argc, char **argv) |
57 | { |
58 | QCoreApplication app(argc, argv); |
59 | QCoreApplication::setApplicationName(QStringLiteral("ksyntaxhighlighter" )); |
60 | QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org" )); |
61 | QCoreApplication::setOrganizationName(QStringLiteral("KDE" )); |
62 | QCoreApplication::setApplicationVersion(QStringLiteral(KSYNTAXHIGHLIGHTING_VERSION_STRING)); |
63 | |
64 | QCommandLineParser parser; |
65 | parser.setApplicationDescription(app.translate(context: "SyntaxHighlightingCLI" , key: "Command line syntax highlighter using KSyntaxHighlighting syntax definitions." )); |
66 | parser.addHelpOption(); |
67 | parser.addVersionOption(); |
68 | parser.addPositionalArgument( |
69 | name: app.translate(context: "SyntaxHighlightingCLI" , key: "source" ), |
70 | description: app.translate(context: "SyntaxHighlightingCLI" , key: "The source file to highlight. If absent, read the file from stdin and the --syntax option must be used." )); |
71 | |
72 | QCommandLineOption listDefs(QStringList() << QStringLiteral("l" ) << QStringLiteral("list" ), |
73 | app.translate(context: "SyntaxHighlightingCLI" , key: "List all available syntax definitions." )); |
74 | parser.addOption(commandLineOption: listDefs); |
75 | QCommandLineOption listThemes(QStringList() << QStringLiteral("list-themes" ), app.translate(context: "SyntaxHighlightingCLI" , key: "List all available themes." )); |
76 | parser.addOption(commandLineOption: listThemes); |
77 | |
78 | QCommandLineOption updateDefs(QStringList() << QStringLiteral("u" ) << QStringLiteral("update" ), |
79 | app.translate(context: "SyntaxHighlightingCLI" , key: "Download new/updated syntax definitions." )); |
80 | parser.addOption(commandLineOption: updateDefs); |
81 | |
82 | QCommandLineOption outputName(QStringList() << QStringLiteral("o" ) << QStringLiteral("output" ), |
83 | app.translate(context: "SyntaxHighlightingCLI" , key: "File to write HTML output to (default: stdout)." ), |
84 | app.translate(context: "SyntaxHighlightingCLI" , key: "output" )); |
85 | parser.addOption(commandLineOption: outputName); |
86 | |
87 | QCommandLineOption syntaxName(QStringList() << QStringLiteral("s" ) << QStringLiteral("syntax" ), |
88 | app.translate(context: "SyntaxHighlightingCLI" , key: "Highlight using this syntax definition (default: auto-detect based on input file)." ), |
89 | app.translate(context: "SyntaxHighlightingCLI" , key: "syntax" )); |
90 | parser.addOption(commandLineOption: syntaxName); |
91 | |
92 | QCommandLineOption themeName(QStringList() << QStringLiteral("t" ) << QStringLiteral("theme" ), |
93 | app.translate(context: "SyntaxHighlightingCLI" , key: "Color theme to use for highlighting." ), |
94 | app.translate(context: "SyntaxHighlightingCLI" , key: "theme" )); |
95 | parser.addOption(commandLineOption: themeName); |
96 | |
97 | QCommandLineOption outputFormatOption(QStringList() << QStringLiteral("f" ) << QStringLiteral("output-format" ), |
98 | app.translate(context: "SyntaxHighlightingCLI" , key: "Use the specified format instead of html. Must be html, ansi or ansi256." ), |
99 | app.translate(context: "SyntaxHighlightingCLI" , key: "format" ), |
100 | QStringLiteral("html" )); |
101 | parser.addOption(commandLineOption: outputFormatOption); |
102 | |
103 | QCommandLineOption traceOption(QStringList() << QStringLiteral("syntax-trace" ), |
104 | app.translate(context: "SyntaxHighlightingCLI" , |
105 | key: "Add information to debug a syntax file. Only works with --output-format=ansi or ansi256. Possible " |
106 | "values are format, region, context, stackSize and all." ), |
107 | app.translate(context: "SyntaxHighlightingCLI" , key: "type" )); |
108 | parser.addOption(commandLineOption: traceOption); |
109 | |
110 | QCommandLineOption noAnsiEditorBg(QStringList() << QStringLiteral("b" ) << QStringLiteral("no-ansi-background" ), |
111 | app.translate(context: "SyntaxHighlightingCLI" , key: "Disable ANSI background for the default color." )); |
112 | parser.addOption(commandLineOption: noAnsiEditorBg); |
113 | |
114 | QCommandLineOption bgRole(QStringList() << QStringLiteral("B" ) << QStringLiteral("background-role" ), |
115 | app.translate(context: "SyntaxHighlightingCLI" , key: "Select background color role from theme." ), |
116 | app.translate(context: "SyntaxHighlightingCLI" , key: "role" )); |
117 | parser.addOption(commandLineOption: bgRole); |
118 | |
119 | QCommandLineOption unbufferedAnsi(QStringList() << QStringLiteral("U" ) << QStringLiteral("unbuffered" ), |
120 | app.translate(context: "SyntaxHighlightingCLI" , key: "For ansi and ansi256 formats, flush the output buffer on each line." )); |
121 | parser.addOption(commandLineOption: unbufferedAnsi); |
122 | |
123 | QCommandLineOption titleOption( |
124 | QStringList() << QStringLiteral("T" ) << QStringLiteral("title" ), |
125 | app.translate(context: "SyntaxHighlightingCLI" , key: "Set HTML page's title\n(default: the filename or \"KSyntaxHighlighter\" if reading from stdin)." ), |
126 | app.translate(context: "SyntaxHighlightingCLI" , key: "title" )); |
127 | parser.addOption(commandLineOption: titleOption); |
128 | |
129 | parser.process(app); |
130 | |
131 | Repository repo; |
132 | |
133 | if (parser.isSet(option: listDefs)) { |
134 | for (const auto &def : repo.definitions()) { |
135 | fprintf(stdout, format: "%s\n" , qPrintable(def.name())); |
136 | } |
137 | return 0; |
138 | } |
139 | |
140 | if (parser.isSet(option: listThemes)) { |
141 | for (const auto &theme : repo.themes()) { |
142 | fprintf(stdout, format: "%s\n" , qPrintable(theme.name())); |
143 | } |
144 | return 0; |
145 | } |
146 | |
147 | Theme::EditorColorRole bgColorRole = Theme::BackgroundColor; |
148 | |
149 | if (parser.isSet(option: bgRole)) { |
150 | /* |
151 | * Theme::EditorColorRole contains border, foreground and background colors. |
152 | * To ensure that only the background colors used in text editing are used, |
153 | * QMetaEnum is avoided and values are listed in hard. |
154 | */ |
155 | |
156 | struct BgRole { |
157 | QStringView name; |
158 | Theme::EditorColorRole role; |
159 | // name for display |
160 | const char *asciiName; |
161 | }; |
162 | |
163 | #define BG_ROLE(role) \ |
164 | BgRole \ |
165 | { \ |
166 | QStringView(u"" #role, sizeof(#role) - 1), Theme::role, #role \ |
167 | } |
168 | constexpr BgRole bgRoles[] = { |
169 | BG_ROLE(BackgroundColor), |
170 | BG_ROLE(TextSelection), |
171 | BG_ROLE(CurrentLine), |
172 | BG_ROLE(SearchHighlight), |
173 | BG_ROLE(ReplaceHighlight), |
174 | BG_ROLE(BracketMatching), |
175 | BG_ROLE(CodeFolding), |
176 | BG_ROLE(MarkBookmark), |
177 | BG_ROLE(MarkBreakpointActive), |
178 | BG_ROLE(MarkBreakpointReached), |
179 | BG_ROLE(MarkBreakpointDisabled), |
180 | BG_ROLE(MarkExecution), |
181 | BG_ROLE(MarkWarning), |
182 | BG_ROLE(MarkError), |
183 | BG_ROLE(TemplateBackground), |
184 | BG_ROLE(TemplatePlaceholder), |
185 | BG_ROLE(TemplateFocusedPlaceholder), |
186 | BG_ROLE(TemplateReadOnlyPlaceholder), |
187 | }; |
188 | #undef BG_ROLE |
189 | |
190 | const auto role = parser.value(option: bgRole); |
191 | bool ok = false; |
192 | for (const auto &def : bgRoles) { |
193 | if (def.name == role) { |
194 | bgColorRole = def.role; |
195 | ok = true; |
196 | break; |
197 | } |
198 | } |
199 | |
200 | if (!ok) { |
201 | fprintf(stderr, format: "Unknown background role. Expected:\n" ); |
202 | for (const auto &def : bgRoles) { |
203 | fprintf(stderr, format: " - %s\n" , def.asciiName); |
204 | } |
205 | return 1; |
206 | } |
207 | } |
208 | |
209 | if (parser.isSet(option: updateDefs)) { |
210 | DefinitionDownloader downloader(&repo); |
211 | QObject::connect(sender: &downloader, signal: &DefinitionDownloader::informationMessage, context: &app, slot: [](const QString &msg) { |
212 | fprintf(stdout, format: "%s\n" , qPrintable(msg)); |
213 | }); |
214 | QObject::connect(sender: &downloader, signal: &DefinitionDownloader::done, context: &app, slot: &QCoreApplication::quit); |
215 | downloader.start(); |
216 | return app.exec(); |
217 | } |
218 | |
219 | bool fromFileName = false; |
220 | QString inFileName; |
221 | if (parser.positionalArguments().size() == 1) { |
222 | fromFileName = true; |
223 | inFileName = parser.positionalArguments().at(i: 0); |
224 | } |
225 | |
226 | Definition def; |
227 | if (parser.isSet(option: syntaxName)) { |
228 | const QString syntax = parser.value(option: syntaxName); |
229 | def = repo.definitionForName(defName: syntax); |
230 | if (!def.isValid()) { |
231 | /* see if it's a mimetype instead */ |
232 | def = repo.definitionForMimeType(mimeType: syntax); |
233 | if (!def.isValid()) { |
234 | /* see if it's a extension instead */ |
235 | def = repo.definitionForFileName(fileName: QLatin1String("f." ) + syntax); |
236 | if (!def.isValid()) { |
237 | /* see if it's a filename instead */ |
238 | def = repo.definitionForFileName(fileName: syntax); |
239 | } |
240 | } |
241 | } |
242 | } else if (fromFileName) { |
243 | def = repo.definitionForFileName(fileName: inFileName); |
244 | } else { |
245 | parser.showHelp(exitCode: 1); |
246 | } |
247 | |
248 | if (!def.isValid()) { |
249 | fprintf(stderr, format: "Unknown syntax.\n" ); |
250 | return 1; |
251 | } |
252 | |
253 | const QString outputFormat = parser.value(option: outputFormatOption); |
254 | if (0 == outputFormat.compare(other: QLatin1String("html" ), cs: Qt::CaseInsensitive)) { |
255 | QString title; |
256 | if (parser.isSet(option: titleOption)) { |
257 | title = parser.value(option: titleOption); |
258 | } |
259 | |
260 | HtmlHighlighter highlighter; |
261 | highlighter.setDefinition(def); |
262 | highlighter.setBackgroundRole(bgColorRole); |
263 | highlighter.setTheme(theme(repo, themeName: parser.value(option: themeName), t: Repository::LightTheme)); |
264 | applyHighlighter(highlighter, parser, fromFileName, inFileName, outputName, highlightParams: title); |
265 | } else { |
266 | auto AnsiFormat = AnsiHighlighter::AnsiFormat::TrueColor; |
267 | // compatible with the old ansi256Colors value |
268 | if (outputFormat.startsWith(s: QLatin1String("ansi256" ), cs: Qt::CaseInsensitive)) { |
269 | AnsiFormat = AnsiHighlighter::AnsiFormat::XTerm256Color; |
270 | } else if (0 != outputFormat.compare(other: QLatin1String("ansi" ), cs: Qt::CaseInsensitive)) { |
271 | fprintf(stderr, format: "Unknown output format.\n" ); |
272 | return 2; |
273 | } |
274 | |
275 | AnsiHighlighter::Options options{}; |
276 | options |= parser.isSet(option: noAnsiEditorBg) ? AnsiHighlighter::Option::NoOptions : AnsiHighlighter::Option::UseEditorBackground; |
277 | options |= parser.isSet(option: unbufferedAnsi) ? AnsiHighlighter::Option::Unbuffered : AnsiHighlighter::Option::NoOptions; |
278 | if (parser.isSet(option: traceOption)) { |
279 | const auto traceOptions = parser.values(option: traceOption); |
280 | for (auto const &option : traceOptions) { |
281 | if (option == QStringLiteral("format" )) { |
282 | options |= AnsiHighlighter::Option::TraceFormat; |
283 | } else if (option == QStringLiteral("region" )) { |
284 | options |= AnsiHighlighter::Option::TraceRegion; |
285 | } else if (option == QStringLiteral("context" )) { |
286 | options |= AnsiHighlighter::Option::TraceContext; |
287 | } else if (option == QStringLiteral("stackSize" )) { |
288 | options |= AnsiHighlighter::Option::TraceStackSize; |
289 | } else if (option == QStringLiteral("all" )) { |
290 | options |= AnsiHighlighter::Option::TraceAll; |
291 | } else { |
292 | fprintf(stderr, format: "Unknown trace name.\n" ); |
293 | return 2; |
294 | } |
295 | } |
296 | } |
297 | |
298 | AnsiHighlighter highlighter; |
299 | highlighter.setDefinition(def); |
300 | highlighter.setBackgroundRole(bgColorRole); |
301 | highlighter.setTheme(theme(repo, themeName: parser.value(option: themeName), t: Repository::DarkTheme)); |
302 | applyHighlighter(highlighter, parser, fromFileName, inFileName, outputName, highlightParams: AnsiFormat, highlightParams: options); |
303 | } |
304 | |
305 | return 0; |
306 | } |
307 | |