summaryrefslogtreecommitdiff
path: root/client/docgen/highlight.cc
blob: 018cf4f0b6bffbdad3a85444d235157a2a59e88c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            highlight.cc
 *
 *  Tue Mar 29 09:12:06 CEST 2011
 *  Copyright 2011 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pracro is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "highlight.h"

#include <QXmlStreamWriter>

typedef enum {
  none,
  intag,
  instring,
  inname,
  inattribute
} xml_state_t;

static bool isChar(QChar c)
{
  return
    (c >= 'a' && c <= 'z') ||
    (c >= 'A' && c <= 'Z') ||
    c == '_';
}
 
static QString highlight_xml(QString code)
{
  QString out;
  xml_state_t state = none;
  bool hasname = false;

  QString::iterator c = code.begin();
  while(c != code.end()) {
    if(*c == '<') {
      out += "<span class=\"brackets\">&lt;</span>";
      state = intag;
      hasname = false;
    } else if(state == intag && *c == '>') {
      out += "<span class=\"brackets\">&gt;</span>";
      state = none;
    } else if(state == intag && *c == '/') {
      out += "<span class=\"brackets\">/</span>";

    } else if(state == intag && hasname == false && isChar(*c)) {
      state = inname;
      hasname = true;
      out += "<span class=\"tag\">" + QString(*c);
    } else if(state == inname && isChar(*c)) {
      out += *c;
    } else if(state == inname) {
      out += "</span>" + QString(*c);
      state = intag;

    } else if(state == intag && hasname == true && isChar(*c)) {
      state = inattribute;
      out += "<span class=\"attribute\">" + QString(*c);
    } else if(state == inattribute && isChar(*c)) {
      out += *c;
    } else if(state == inattribute) {
      out += "</span>" + QString(*c);
      state = intag;

    } else if(state == intag && *c == '\"') {
      state = instring;
      out += "<span class=\"string\">&quot;";
    } else if(state == instring && *c == '\"') {
      state = intag;
      out += "&quot;</span>";
    } else if(state == instring) {
      out += *c;

    } else out += *c;
    c++;
  }
  
  /*
  QXmlStreamWriter w(&e->code);
  w.writeCharacters(line + "\n");
  */

  return out;
}

static bool isLuaChar(QChar c)
{
  return
    (c >= 'a' && c <= 'z') ||
    (c >= 'A' && c <= 'Z') ||
    c == '_';
}

static bool isKeyword(QString code)
{
  return code == "if" ||
    code == "then" ||
    code == "else" ||
    code == "end" ||
    code == "this" ||
    code == "function" ||
    code == "local" ||
    code == "and" ||
    code == "or" ||
    code == "not";
}

static QString highlight_lua(QString code)
{
  QString out;
  QString word;

  bool instring = false;

  QString::iterator c = code.begin();
  while(c != code.end()) {
    if(isLuaChar(*c)) {
      word += *c;
    } else {
      if(instring && *c == '\'') {
        out += word + "'</span>";
        instring = false;
      } else if(instring) {
        out += word + QString(*c);
      } else if(isKeyword(word)) {
        out += "<span class=\"keyword\">"+word+"</span>" + QString(*c);
      } else if(*c == '(') {
        out += "<span class=\"function\">"+word+"</span>" + QString(*c);
      } else if(instring == false && *c == '\'') {
        out += word + "<span class=\"string\">'";
        instring = true;
      } else {
        out += word + QString(*c);
      }
      word = "";
    }
    
    c++;
  }

  return out;
}

QString highlight(QString code, lang_t lang)
{
  switch(lang) {
  case lua:
    return highlight_lua(code);
  case xml:
    return highlight_xml(code);
  }
  return "";
}