diff options
author | albert-github <albert.tests@gmail.com> | 2021-01-17 12:54:35 (GMT) |
---|---|---|
committer | albert-github <albert.tests@gmail.com> | 2021-01-17 12:54:35 (GMT) |
commit | 4fd8254c903b251be91ab669f4d83cb86ebaf499 (patch) | |
tree | 09f9d49c1cba95de282ee9a5359e60c201f71ccc /src | |
parent | 37dbd4bdf65d21dba0d81940e74801a5504f9c34 (diff) | |
download | Doxygen-4fd8254c903b251be91ab669f4d83cb86ebaf499.zip Doxygen-4fd8254c903b251be91ab669f4d83cb86ebaf499.tar.gz Doxygen-4fd8254c903b251be91ab669f4d83cb86ebaf499.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/docbookvisitor.cpp | 9 | ||||
-rw-r--r-- | src/htmldocvisitor.cpp | 2 | ||||
-rw-r--r-- | src/latexdocvisitor.cpp | 2 | ||||
-rw-r--r-- | src/mandocvisitor.cpp | 4 | ||||
-rw-r--r-- | src/rtfdocvisitor.cpp | 4 | ||||
-rw-r--r-- | src/util.cpp | 73 | ||||
-rw-r--r-- | src/util.h | 3 | ||||
-rw-r--r-- | 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 << "<literallayout><computeroutput>"; 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) @@ -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 << "<programlisting"; if (!s->language().isEmpty()) m_t << " filename=\"" << lang << "\">"; |