"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bHighlighter.cpp" (12 Feb 2021, 5020 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "c2bHighlighter.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "c2bHighlighter.h"
    8 #include "c2bSettings.h"
    9 
   10 #include <QPalette>
   11 #include <QTextDocument>
   12 
   13 
   14 c2bHighlighter::c2bHighlighter(QTextDocument* doc) : QSyntaxHighlighter(doc)
   15 {
   16     QPalette p;
   17     text_color = p.text().color();
   18     c2bSettings* settings(c2bSettingsP);
   19     c = &settings->colors;
   20     connect(settings, SIGNAL(newSettings()), this, SLOT(rehighlight()));
   21 
   22     rx0 = QRegExp("\\d+");
   23     rx0.setMinimal(true);
   24     rx1 = QRegExp("[^\\w\\d]+");
   25     rx1.setMinimal(true);
   26     rx2 = QRegExp("<(?:NewLine|Tab)\\d+>");
   27     rx2.setMinimal(true);
   28     rx3 = QRegExp("\\b(?:19|20)\\d\\d\\b");
   29     rx3.setMinimal(true);
   30     rx4 = QRegExp("\\b(?:abstract|authors{0,1}|introduction|issue|keywords|key words|title|volume|vol)\\b",
   31                   Qt::CaseInsensitive);
   32     rx4.setMinimal(true);
   33     rx5 = QRegExp("<<(?:abstract|address|annote|author|booktitle|chapter|"
   34                   "doi|edition|editor|eprint|file|institution|isbn|issn|journal|"
   35                   "keywords|month|note|number|organization|pages|publisher|"
   36                   "school|series|title|url|volume|year)>>");
   37     rx5.setMinimal(true);
   38     rx6 = QRegExp("<AnyPattern>");
   39     rx6.setMinimal(true);
   40     rx_extracted_starts = QRegExp("\\[\\[");
   41     rx_extracted_ends = QRegExp("\\]\\]");
   42     rx_metadata_starts = QRegExp("\\[(Bibliographic|Raw) Metadata");
   43     rx_metadata_ends = QRegExp("/(Bibliographic|Raw) Metadata\\]");
   44 }
   45 
   46 c2bHighlighter::~c2bHighlighter() {}
   47 
   48 
   49 void c2bHighlighter::highlightBlock(const QString& text)
   50 {
   51     const int len(text.length());
   52     setFormat(0, len - 1, text_color);
   53 
   54     int pos(0);
   55     while (pos >= 0)
   56     {
   57         pos = rx0.indexIn(text, pos);
   58         if (pos > -1)
   59         {
   60             setFormat(pos, rx0.matchedLength(), c->cb2bib_digit_color);
   61             pos += rx0.matchedLength();
   62         }
   63     }
   64     pos = 0;
   65     while (pos >= 0)
   66     {
   67         pos = rx1.indexIn(text, pos);
   68         if (pos > -1)
   69         {
   70             setFormat(pos, rx1.matchedLength(), c->cb2bib_unrelevant_color);
   71             pos += rx1.matchedLength();
   72         }
   73     }
   74     pos = 0;
   75     while (pos >= 0)
   76     {
   77         pos = rx2.indexIn(text, pos);
   78         if (pos > -1)
   79         {
   80             setFormat(pos, rx2.matchedLength(), c->cb2bib_unrelevant_color);
   81             pos += rx2.matchedLength();
   82         }
   83     }
   84     pos = 0;
   85     while (pos >= 0)
   86     {
   87         pos = rx3.indexIn(text, pos);
   88         if (pos > -1)
   89         {
   90             setFormat(pos, rx3.matchedLength(), c->cb2bib_relevant_color);
   91             pos += rx3.matchedLength();
   92         }
   93     }
   94     pos = 0;
   95     while (pos >= 0)
   96     {
   97         pos = rx4.indexIn(text, pos);
   98         if (pos > -1)
   99         {
  100             setFormat(pos, rx4.matchedLength(), c->cb2bib_highly_relevant_color);
  101             pos += rx4.matchedLength();
  102         }
  103     }
  104     pos = 0;
  105     while (pos >= 0)
  106     {
  107         pos = rx5.indexIn(text, pos);
  108         if (pos > -1)
  109         {
  110             setFormat(pos, rx5.matchedLength(), c->cb2bib_tag_color);
  111             pos += rx5.matchedLength();
  112         }
  113     }
  114     pos = 0;
  115     while (pos >= 0)
  116     {
  117         pos = rx6.indexIn(text, pos);
  118         if (pos > -1)
  119         {
  120             setFormat(pos, rx6.matchedLength(), c->cb2bib_unrelevant_color);
  121             pos += rx6.matchedLength();
  122         }
  123     }
  124 
  125     setCurrentBlockState(0);
  126     int startIndexMD(0);
  127     if (previousBlockState() != 1)
  128         startIndexMD = rx_metadata_starts.indexIn(text);
  129     while (startIndexMD >= 0)
  130     {
  131         const int endIndex(rx_metadata_ends.indexIn(text, startIndexMD));
  132         int extractedLength;
  133         if (endIndex == -1)
  134         {
  135             setCurrentBlockState(1);
  136             extractedLength = text.length() - startIndexMD;
  137         }
  138         else
  139             extractedLength = endIndex - startIndexMD + rx_metadata_ends.matchedLength();
  140         setFormat(startIndexMD, extractedLength, c->cb2bib_metadata_color);
  141         startIndexMD = rx_metadata_starts.indexIn(text, startIndexMD + extractedLength);
  142     }
  143 
  144     int startIndexEx(0);
  145     if (previousBlockState() != 2)
  146         startIndexEx = rx_extracted_starts.indexIn(text);
  147     while (startIndexEx >= 0)
  148     {
  149         const int endIndex(rx_extracted_ends.indexIn(text, startIndexEx));
  150         int extractedLength;
  151         if (endIndex == -1)
  152         {
  153             setCurrentBlockState(2);
  154             extractedLength = text.length() - startIndexEx;
  155         }
  156         else
  157             extractedLength = endIndex - startIndexEx + rx_extracted_ends.matchedLength();
  158         setFormat(startIndexEx, extractedLength, c->cb2bib_unrelevant_color);
  159         startIndexEx = rx_extracted_starts.indexIn(text, startIndexEx + extractedLength);
  160     }
  161 }