about summary refs log tree commit diff stats
path: root/linkify/003linkify.cc
blob: 929ea13baccca1edd7843493ee809cecf4c2984f (plain) (blame)
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
// Read a tabular cross-reference file generated by ctags, then read a list of
// html files generated by Vim's TOhtml command on C++ code. Link words
// in the html files to cross-references from ctags.

// Usage:
//    linkify [tags file] [html files]...

// Still plenty of holes:
// - unnecessarily linking definition location to itself
// - can't detect strings in spite of attempt to support them below, because
//   Vim's generated html turns quotes into html entities
// - distinguishing function and variable names
// - distinguishing Mu code in C++ files
// - distinguishing between function overloads
//   - if there's duplicate tags we aren't smart enough to distinguish between
//     them yet, so we simply don't add any link at all
//   - but even that's not perfect, because sometimes the tags file has a
//     single definition but there's still multiple overloads (say I defined
//     'clear()' on some type, and it's already defined on STL classes)

struct syminfo {
  string filename;
  int line_num;
  syminfo() :line_num(0) {}
};

bool has_data(istream& in) {
  in.peek();
  if (in.eof()) return false;
  assert(in);
  return true;
}

bool starts_with(const string& s, const string& pat) {
  string::const_iterator a=s.begin(), b=pat.begin();
  for (/*nada*/;  a!=s.end() && b!=pat.end();  ++a, ++b)
    if (*a != *b) return false;
  return b == pat.end();
}

void read_tags(const string& filename, map<string, syminfo>& info) {
  ifstream in(filename);
//&   cerr << "reading " << filename << '\n';
  string dummy;
  while (has_data(in)) {
    string symbol;  in >> symbol;
//&     cerr << symbol << '\n';
    if (info.find(symbol) != info.end()) {
      info[symbol].line_num = -1;
      info[symbol].filename.clear();
    }
    else {
      in >> dummy;
      in >> info[symbol].line_num;
      in >> info[symbol].filename;
    }
    getline(in, dummy);  // skip rest of line
//&     cerr << symbol << ": " << info[symbol].filename << ':' << info[symbol].line_num << '\n';
  }
  in.close();
}

void replace_tags_in_file(const string& filename, const map<string, syminfo>& info) {
//&   cerr << info.size() << " symbols\n";
  ifstream in(filename);
  ofstream out(filename+".out");
  while (has_data(in)) {
    // send lines that don't start with '<span' straight through
    string line;
    getline(in, line);
    if (!starts_with(line, "<span ")) {
      out << line << '\n';
    }
    else {
      static int span_size = string("</span>").size();
      int skip_first_span = line.find("</span>") + span_size;
      out << line.substr(0, skip_first_span);
      istringstream in2(line.substr(skip_first_span));
      in2 >> std::noskipws;
      while (has_data(in2)) {
        if (isspace(in2.peek())) {
//&           cerr << "space\n";
          char c;  in2 >> c;
          out << c;
        }
        // within a line, send straight through all characters inside '<..>'
        else if (in2.peek() == '<') {
//&           cerr << "tag\n";
          char c = '\0';
          while (in2 >> c) {
//&             cerr << "span: " << c << '\n';
            out << c;
            if (c == '>') break;
          }
//&           cerr << "end tag\n";
        }
        else {
          // send straight through all characters inside strings (handling escapes)
          char c = in2.get();
          if (c == '"') {
//&             cerr << "string\n";
            out << c;
            while (in2 >> c) {
              out << c;
              if (c == '\\') {
                in2 >> c;  out << c;
              }
              else if (c == '"') {
                break;
              }
            }
          }
          else if (c == '\'') {
//&             cerr << "character\n";
            out << c;
            while (in2 >> c) {
              out << c;
              if (c == '\\') {
                in2 >> c;  out << c;
              }
              else if (c == '\'') {
                break;
              }
            }
          }
          // send straight through any characters after '//' (comments)
          else if (c == '#') {
//&             cerr << "comment\n";
            out << c;
            while (in2 >> c) out << c;
          }
          // send straight through any characters after '//' (comments)
          else if (c == '/' && in2.peek() == '/') {
//&             cerr << "comment\n";
            out << c;
            while (in2 >> c) out << c;
          }
          else {
//&             cerr << "rest\n";
            if (c == ',' || c == ':') {
              out << c;
              continue;
            }
            ostringstream out2;
            out2 << c;
            while (in2 >> c) {
              if (isspace(c) || c == '<' || c == '"' || c == '\'' || c == '/' || c == ',' || c == ':') {  // keep sync'd with other clauses above
                in2.putback(c);
                break;
              }
              out2 << c;
            }
            string symbol = out2.str();
            if (symbol == "equal" || symbol == "index" || symbol == "put-index" || symbol == "length") {
//&               cerr << "  blacklisted\n";
              out << symbol;
            }
            else if (info.find(symbol) == info.end()) {
//&               cerr << "  no info\n";
              out << symbol;
            }
            else {
              const syminfo& s = info.find(symbol)->second;
              if (s.filename.empty()) {
//&                 cerr << "  empty info\n";
                out << symbol;
              }
              else {
//&                 cerr << "  link\n";
                out << "<a href='" << s.filename << ".html#L" << s.line_num << "'>" << symbol << "</a>";
              }
            }
          }  // end rest
        }
      }  // done parsing line
      out << '\n';
    }
  }
  in.close();  out.close();
}

int linkify(int argc, const char* argv[]) {
  map<string, syminfo> info;
  read_tags(argv[1], info);
  for (int i = 2;  i < argc;  ++i) {
    replace_tags_in_file(argv[i], info);
  }
  return 0;
}