summaryrefslogtreecommitdiff
path: root/client/docgen/generate.cc
blob: 5cff6b745b7cfa8d69f86dc53c314609c3f95839 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            generate.cc
 *
 *  Thu Mar 24 12:09:33 CET 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 "generate.h"

#include <QTextStream>
#include <QDate>
#include <QStringList>

#include "genimage.h"

static QString function(QString f)
{
  QString out;

  if(f.indexOf(" ") > f.indexOf("(") || f.indexOf(" ") == -1) {
    printf("MISSING return type in \"%s\".\n", f.toStdString().c_str());
    return f;
  }

  QString ret = f.left(f.indexOf(" "));
  QString name = f.mid(f.indexOf(" ") + 1, f.indexOf("(") - f.indexOf(" ") - 1);
  QString ps = f.mid(f.indexOf("(") + 1,
                        f.indexOf(")") - f.indexOf("(") - 1);

  QStringList parms = ps.split(",");

  out += "<span class=\"luatype\">" + ret + "</span> ";
  out += "<span class=\"luamethod\">" + name + "</span>";
  out += "(<span class=\"luaparms\">";
  for(int i = 0; i < parms.size(); i++) {
    QStringList l = parms.at(i).split(" ", QString::SkipEmptyParts);
    if(l.length() != 2) {
      out += parms.at(i);
      continue;
    }
    QString type = l.at(0);
    QString name = l.at(1);
    if(i != 0) out += ", ";
    out += "<span class=\"luatype\">" + type + "</span>";
    out += " <span class=\"luaparm\">" + name + "</span>";
  }
  out += "</span>)";
  

  return out;
}

static QString generateMethods(QVector<Method> meths)
{
  QString out;

  foreach(Method meth, meths) {
    out += "        <div class=\"method\">\n";
    out += "          <a name=\"" + meth.name +
      "\"></a><div class=\"name\">" + function(meth.name) + "</div>\n";
    out += "          <div class=\"description\">" + meth.description +
      "</div>\n";
    
    if(meth.parameters.size()) {
      out += "          <div class=\"parameters\">\n";
      foreach(Parameter parm, meth.parameters) {
        out += "            <div class=\"parameter\">\n";
        out += "              <div class=\"name\">" + parm.name + "</div>\n";
        out += "              <div class=\"description\">" +
          parm.description + "</div>\n";
        out += "            </div>\n";
      }
      out += "          </div>\n";
    }
    
    if(meth.returns != "") {
      out += "          <div class=\"returns\"><strong>Returns</strong> " +
        meth.returns + "</div>\n";
    }
    
    out += "        </div>\n";
  }

  return out;
}

static QString generateAttributes(QVector<Attribute> attrs)
{
  QString out;

  foreach(Attribute attr, attrs) {
    out += "        <div class=\"attribute\">\n";
    out += "          <div class=\"name\">" + attr.name + "</div>\n";
    out += "          <div class=\"description\">" + attr.description +
      "</div>\n";
    out += "        </div>\n";
  }

  return out;
}

static QString generateMethodOverview(Doc &doc,
                                      QMap<QString, QVector<Method> > &imeths)
                               
{
  QString out;
  
  out += "<h2>Method overview</h2>\n";

  QVector<Method> meths;

  meths += doc.methods;

  QMap<QString, QVector<Method> >::iterator j = imeths.begin();
  while(j != imeths.end()) {
    meths += j.value();
    j++;
  }

  // sort
  for(int i = 0; i < meths.size(); i++) {
    for(int j = 0; j < meths.size(); j++) {
      if(meths[i].name < meths[j].name) {
        Method tmp = meths[i];
        meths[i] = meths[j];
        meths[j] = tmp;
      }
    }
  }

  out += "<ul>\n";
  foreach(Method meth, meths) {
    out += "<li><a href=\"#"+meth.name+"\">"+meth.name+"</a> " +
      meth.description.left(meth.description.indexOf(".") + 1) + "</li>\n";
  }
  out += "</ul>\n";

  return out;
}

QString generate(Doc &doc, QMap<QString, QVector<Method> > &meths,
                 QMap<QString, QVector<Attribute> > &atts)
{
  QString out;

  out += "    <div class=\"header\"><a href=\"index.html\">Overview</a>"
    "</div>\n";

  out += "    <div class=\"doc\">\n";
  out += "      <h1>" + doc.title + "</h1>\n";
  if(doc.tag != "" ){
    out += "      <div class=\"tagname\">&lt;" + doc.tag + " /&gt;</div>\n";
  }

  if(doc.container) {
    out += "      <div class=\"container\">Widget is a container.</div>\n";
  }

  if(doc.extends != "") {
    out += "      <div class=\"extends\">Extends: <a href=\""+doc.extends+
      ".html\">" + doc.extends + "</a></div>\n";
  }

  if(doc.screenshot) {
    genImage(doc.tag);
    out += "      <div class=\"screenshot\">\n";
    out += "        <img src=\"gfx/" + doc.tag + ".png\"/>\n";
    out += "      </div>\n";
  }

  out += "      <div class=\"description\">\n";
  out += doc.description;
  out += "      </div>\n";

  out += "      <h2>Attributes</h2>\n";
  out += "      <div class=\"attributes\">\n";
  if(doc.attributes.size()) {
    out += generateAttributes(doc.attributes);
  }

  QMap<QString, QVector<Attribute> >::iterator i = atts.begin();
  while(i != atts.end()) {
    out += "      <h3>Attributes inherited from <a href=\"" + i.key() +
      ".html\">"+i.key()+"</a>:</h3>\n";
    out += generateAttributes(i.value());
    i++;
  }
  out += "      </div>\n";
 
  out += generateMethodOverview(doc, meths);

  out += "      <h2>Methods</h2>\n";
  out += "      <div class=\"methods\">\n";
  if(doc.methods.size()) {
    out += generateMethods(doc.methods);
  }

  QMap<QString, QVector<Method> >::iterator j = meths.begin();
  while(j != meths.end()) {
    out += "      <h3>Methods inherited from <a href=\"" + j.key() +
      ".html\">"+j.key()+"</a>:</h3>\n";
    out += generateMethods(j.value());
    j++;
  }
  out += "      </div>\n";

  out += "    </div>\n";

  out += "    <div class=\"footer\">This documentation is generated for"
    " <a href=\"http://www.aasimon.org/pracro\">Pracro</a>"
    " version "VERSION" at "+QDate::currentDate().toString()+"</div>\n";

  return out;
}

#ifdef TEST_GENERATE
//Additional dependency files
//deps:
//Required cflags (autoconf vars may be used)
//cflags:
//Required link options (autoconf vars may be used)
//libs:
#include "test.h"

TEST_BEGIN;

// TODO: Put some testcode here (see test.h for usable macros).
TEST_TRUE(false, "No tests yet!");

TEST_END;

#endif/*TEST_GENERATE*/