From 4fd8254c903b251be91ab669f4d83cb86ebaf499 Mon Sep 17 00:00:00 2001 From: albert-github Date: Sun, 17 Jan 2021 13:54:35 +0100 Subject: Use language name to get code coloring In a `\code` or `~~~` or ` ``` ` environment we can define the language of a code block by specifying an extension. In markdown we see quite often the language name as code block name (especially for `python`) One can specify it by means of an `EXTENSION_MAPPING` but this is quite unnatural as it it not an extension. - see to it that the extension (and thus `EXTENSION_MAPPING`) still has precedence - in case not a known extension, try the language names. Note: with docbook the explicit name usage was missing. --- src/docbookvisitor.cpp | 9 ++++-- src/htmldocvisitor.cpp | 2 +- src/latexdocvisitor.cpp | 2 +- src/mandocvisitor.cpp | 4 +-- src/rtfdocvisitor.cpp | 4 +-- src/util.cpp | 73 +++++++++++++++++++++++++++++++++++-------------- src/util.h | 3 +- src/xmldocvisitor.cpp | 4 +-- 8 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/docbookvisitor.cpp b/src/docbookvisitor.cpp index d444b7b..cde2da6 100644 --- a/src/docbookvisitor.cpp +++ b/src/docbookvisitor.cpp @@ -311,10 +311,15 @@ void DocbookDocVisitor::visit(DocVerbatim *s) { DB_VIS_C if (m_hide) return; - SrcLangExt langExt = getLanguageFromFileName(m_langExt); + QCString lang = m_langExt; + if (!s->language().isEmpty()) // explicit language setting + { + lang = s->language(); + } + SrcLangExt langExt = getLanguageFromCodeLang(lang); switch(s->type()) { - case DocVerbatim::Code: // fall though + case DocVerbatim::Code: m_t << ""; getCodeParser(m_langExt).parseCode(m_ci,s->context(), s->text(), diff --git a/src/htmldocvisitor.cpp b/src/htmldocvisitor.cpp index b93a2c4..fc3ca57 100644 --- a/src/htmldocvisitor.cpp +++ b/src/htmldocvisitor.cpp @@ -517,7 +517,7 @@ void HtmlDocVisitor::visit(DocVerbatim *s) { lang = s->language(); } - SrcLangExt langExt = getLanguageFromFileName(lang); + SrcLangExt langExt = getLanguageFromCodeLang(lang); switch(s->type()) { case DocVerbatim::Code: diff --git a/src/latexdocvisitor.cpp b/src/latexdocvisitor.cpp index e6c9363..f736498 100644 --- a/src/latexdocvisitor.cpp +++ b/src/latexdocvisitor.cpp @@ -346,7 +346,7 @@ void LatexDocVisitor::visit(DocVerbatim *s) { lang = s->language(); } - SrcLangExt langExt = getLanguageFromFileName(lang); + SrcLangExt langExt = getLanguageFromCodeLang(lang); switch(s->type()) { case DocVerbatim::Code: diff --git a/src/mandocvisitor.cpp b/src/mandocvisitor.cpp index 875cd14..95d15f8 100644 --- a/src/mandocvisitor.cpp +++ b/src/mandocvisitor.cpp @@ -195,10 +195,10 @@ void ManDocVisitor::visit(DocVerbatim *s) { lang = s->language(); } - SrcLangExt langExt = getLanguageFromFileName(lang); + SrcLangExt langExt = getLanguageFromCodeLang(lang); switch (s->type()) { - case DocVerbatim::Code: // fall though + case DocVerbatim::Code: if (!m_firstCol) m_t << endl; m_t << ".PP" << endl; m_t << ".nf" << endl; diff --git a/src/rtfdocvisitor.cpp b/src/rtfdocvisitor.cpp index 2bd2c29..08d5b63 100644 --- a/src/rtfdocvisitor.cpp +++ b/src/rtfdocvisitor.cpp @@ -299,10 +299,10 @@ void RTFDocVisitor::visit(DocVerbatim *s) { lang = s->language(); } - SrcLangExt langExt = getLanguageFromFileName(lang); + SrcLangExt langExt = getLanguageFromCodeLang(lang); switch(s->type()) { - case DocVerbatim::Code: // fall though + case DocVerbatim::Code: m_t << "{" << endl; m_t << "\\par" << endl; m_t << rtf_Style_Reset << getStyle("CodeExample"); diff --git a/src/util.cpp b/src/util.cpp index 3feb8db..e4c32c3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -5539,29 +5539,30 @@ static struct Lang2ExtMap const char *langName; const char *parserName; SrcLangExt parserId; + const char *defExt; } g_lang2extMap[] = { // language parser parser option - { "idl", "c", SrcLangExt_IDL }, - { "java", "c", SrcLangExt_Java }, - { "javascript", "c", SrcLangExt_JS }, - { "csharp", "c", SrcLangExt_CSharp }, - { "d", "c", SrcLangExt_D }, - { "php", "c", SrcLangExt_PHP }, - { "objective-c", "c", SrcLangExt_ObjC }, - { "c", "c", SrcLangExt_Cpp }, - { "c++", "c", SrcLangExt_Cpp }, - { "slice", "c", SrcLangExt_Slice }, - { "python", "python", SrcLangExt_Python }, - { "fortran", "fortran", SrcLangExt_Fortran }, - { "fortranfree", "fortranfree", SrcLangExt_Fortran }, - { "fortranfixed", "fortranfixed", SrcLangExt_Fortran }, - { "vhdl", "vhdl", SrcLangExt_VHDL }, - { "xml", "xml", SrcLangExt_XML }, - { "sql", "sql", SrcLangExt_SQL }, - { "md", "md", SrcLangExt_Markdown }, - { 0, 0, (SrcLangExt)0 } + { "idl", "c", SrcLangExt_IDL, ".idl" }, + { "java", "c", SrcLangExt_Java, ".java"}, + { "javascript", "c", SrcLangExt_JS, ".js" }, + { "csharp", "c", SrcLangExt_CSharp, ".cs" }, + { "d", "c", SrcLangExt_D, ".d" }, + { "php", "c", SrcLangExt_PHP, ".php" }, + { "objective-c", "c", SrcLangExt_ObjC, ".m" }, + { "c", "c", SrcLangExt_Cpp, ".c" }, + { "c++", "c", SrcLangExt_Cpp, ".cpp" }, + { "slice", "c", SrcLangExt_Slice, ".ice" }, + { "python", "python", SrcLangExt_Python, ".py" }, + { "fortran", "fortran", SrcLangExt_Fortran, ".f" }, + { "fortranfree", "fortranfree", SrcLangExt_Fortran, ".f90" }, + { "fortranfixed", "fortranfixed", SrcLangExt_Fortran, ".f" }, + { "vhdl", "vhdl", SrcLangExt_VHDL, ".vhdl"}, + { "xml", "xml", SrcLangExt_XML, ".xml" }, + { "sql", "sql", SrcLangExt_SQL, ".sql" }, + { "md", "md", SrcLangExt_Markdown, ".md" }, + { 0, 0, (SrcLangExt)0, 0 } }; bool updateLanguageMapping(const QCString &extension,const QCString &language) @@ -5666,7 +5667,7 @@ void addCodeOnlyMappings() updateLanguageMapping(".sql", "sql"); } -SrcLangExt getLanguageFromFileName(const QCString& fileName) +SrcLangExt getLanguageFromFileName(const QCString& fileName, SrcLangExt defLang) { QFileInfo fi(fileName); // we need only the part after the last ".", newer implementations of QFileInfo have 'suffix()' for this. @@ -5680,7 +5681,37 @@ SrcLangExt getLanguageFromFileName(const QCString& fileName) return (SrcLangExt)*pVal; } //printf("getLanguageFromFileName(%s) not found!\n",fileName.data()); - return SrcLangExt_Cpp; // not listed => assume C-ish language. + return defLang; // not listed => assume default specified language +} + +/// Routine to handle the language attribute of the `\code` command +SrcLangExt getLanguageFromCodeLang(QCString &fileName) +{ + // try the extension + SrcLangExt lang = getLanguageFromFileName(fileName, SrcLangExt_Unknown); + if (lang == SrcLangExt_Unknown) + { + // try the language names + const Lang2ExtMap *p = g_lang2extMap; + QCString langName = fileName.lower(); + if (langName.at(0)=='.') langName = langName.mid(1); + while (p->langName) + { + if (langName==p->langName) + { + // found the language + lang = p->parserId; + fileName = p->defExt; + break; + } + p++; + } + if (!p->langName) + { + return SrcLangExt_Cpp; + } + } + return lang; } QCString getFileNameExtension(QCString fn) diff --git a/src/util.h b/src/util.h index 6d50d88..946b9f6 100644 --- a/src/util.h +++ b/src/util.h @@ -362,7 +362,8 @@ bool findAndRemoveWord(QCString &s,const QCString &word); QCString stripLeadingAndTrailingEmptyLines(const QCString &s,int &docLine); bool updateLanguageMapping(const QCString &extension,const QCString &parser); -SrcLangExt getLanguageFromFileName(const QCString& fileName); +SrcLangExt getLanguageFromFileName(const QCString& fileName, SrcLangExt defLang=SrcLangExt_Cpp); +SrcLangExt getLanguageFromCodeLang(QCString &fileName); QCString getFileNameExtension(QCString fn); void initDefaultExtensionMapping(); void addCodeOnlyMappings(); diff --git a/src/xmldocvisitor.cpp b/src/xmldocvisitor.cpp index 29990c9..3178328 100644 --- a/src/xmldocvisitor.cpp +++ b/src/xmldocvisitor.cpp @@ -247,10 +247,10 @@ void XmlDocVisitor::visit(DocVerbatim *s) { lang = s->language(); } - SrcLangExt langExt = getLanguageFromFileName(lang); + SrcLangExt langExt = getLanguageFromCodeLang(lang); switch(s->type()) { - case DocVerbatim::Code: // fall though + case DocVerbatim::Code: m_t << "language().isEmpty()) m_t << " filename=\"" << lang << "\">"; -- cgit v0.12