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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
|
#!/usr/bin/env python
# -*- Mode: Python -*-
# GObject-Introspection - a framework for introspecting GObject libraries
# Copyright (C) 2010 Zach Goldberg
# Copyright (C) 2011 Johan Dahlin
# Copyright (C) 2011 Shaun McCance
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
import os
import re
import sys
import tempfile
from xml.sax import saxutils
from mako.lookup import TemplateLookup
import markdown
from markdown.extensions.headerid import HeaderIdExtension
from . import ast, xmlwriter
from .utils import to_underscores
from .mdextensions import InlineMarkdown
# Freely inspired from
# https://github.com/GNOME/yelp-xsl/blob/master/js/syntax.html
language_mimes = {
"bash-script": "application/x-shellscript",
"shell": "application/x-shellscript",
"csharp": "text/x-csharp",
"css": "text/css",
"diff": "text/xpatch",
"html": "text/html",
"java": "text/x-java",
"javascript": "application/javascript",
"lisp": "text/x-scheme",
"lua": "text-x-lua",
"c": "text/x-csrc",
"c++": "text/x-c++src",
"pascal": "text/x-pascal",
"perl": "application/x-perl",
"php": "application/x-php",
"plain": "text/plain",
"python": "text/x-python",
"ruby": "application/x-ruby",
"sql": "text/x-sql",
"yaml": "application/x-yaml",
}
def make_page_id(node, recursive=False):
if isinstance(node, ast.Namespace):
if recursive:
return node.name
else:
return 'index'
if hasattr(node, '_chain') and node._chain:
parent = node._chain[-1]
else:
parent = getattr(node, 'parent', None)
if parent is None:
if isinstance(node, ast.Function) and node.shadows:
return '%s.%s' % (node.namespace.name, node.shadows)
else:
return '%s.%s' % (node.namespace.name, node.name)
if isinstance(node, (ast.Property, ast.Signal, ast.VFunction, ast.Field)):
return '%s-%s' % (make_page_id(parent, recursive=True), node.name)
elif isinstance(node, ast.Function) and node.shadows:
return '%s.%s' % (make_page_id(parent, recursive=True), node.shadows)
else:
return '%s.%s' % (make_page_id(parent, recursive=True), node.name)
def get_node_kind(node):
if isinstance(node, ast.Namespace):
node_kind = 'namespace'
elif isinstance(node, (ast.Class, ast.Boxed, ast.Compound)):
node_kind = 'class'
elif isinstance(node, ast.Interface):
node_kind = 'interface'
elif isinstance(node, ast.Record):
node_kind = 'record'
elif isinstance(node, ast.Function):
if node.is_method:
node_kind = 'method'
elif node.is_constructor:
node_kind = 'constructor'
else:
node_kind = 'function'
elif isinstance(node, (ast.Enum, ast.Bitfield)):
node_kind = 'enum'
elif isinstance(node, ast.Property) and node.parent is not None:
node_kind = 'property'
elif isinstance(node, ast.Signal) and node.parent is not None:
node_kind = 'signal'
elif isinstance(node, ast.VFunction) and node.parent is not None:
node_kind = 'vfunc'
elif isinstance(node, ast.Callable):
node_kind = 'callback'
elif isinstance(node, ast.Field):
node_kind = 'field'
else:
node_kind = 'default'
return node_kind
class TemplatedScanner(object):
def __init__(self, specs):
self.specs = self.unmangle_specs(specs)
self.regex = self.make_regex(self.specs)
def unmangle_specs(self, specs):
mangled = re.compile('<<([a-zA-Z_:]+)>>')
specdict = dict((name.lstrip('!'), spec) for name, spec in specs)
def unmangle(spec, name=None):
def replace_func(match):
child_spec_name = match.group(1)
if ':' in child_spec_name:
pattern_name, child_spec_name = child_spec_name.split(':', 1)
else:
pattern_name = None
child_spec = specdict[child_spec_name]
# Force all child specs of this one to be unnamed
unmangled = unmangle(child_spec, None)
if pattern_name and name:
return '(?P<%s_%s>%s)' % (name, pattern_name, unmangled)
else:
return unmangled
return mangled.sub(replace_func, spec)
return [(name, unmangle(spec, name)) for name, spec in specs]
def make_regex(self, specs):
regex = '|'.join('(?P<%s>%s)' % (name, spec) for name, spec in specs
if not name.startswith('!'))
return re.compile(regex)
def get_properties(self, name, match):
groupdict = match.groupdict()
properties = {name: groupdict.pop(name)}
name = name + "_"
for group, value in groupdict.items():
if group.startswith(name):
key = group[len(name):]
properties[key] = value
return properties
def scan(self, text):
pos = 0
while True:
match = self.regex.search(text, pos)
if match is None:
break
start = match.start()
if start > pos:
yield ('other', text[pos:start], None)
pos = match.end()
name = match.lastgroup
yield (name, match.group(0), self.get_properties(name, match))
if pos < len(text):
yield ('other', text[pos:], None)
class DocstringScanner(TemplatedScanner):
def __init__(self):
specs = [
('!alpha', r'[a-zA-Z0-9_]+'),
('!alpha_dash', r'[a-zA-Z0-9_-]+'),
('code_start_with_language',
r'\|\[\<!\-\-\s*language\s*\=\s*\"<<language_name:alpha>>\"\s*\-\-\>'),
('code_start', r'\|\['),
('code_end', r'\]\|'),
('html_code_start', r'<code(.*?)>'),
('html_code_end', r'</code>'),
('markdown_code_toggle', r'\`'),
('markdown_attr_start', r'\{'),
('markdown_attr_end', r'\}'),
('property', r'#<<type_name:alpha>>:(<<property_name:alpha_dash>>)'),
('signal', r'#<<type_name:alpha>>::(<<signal_name:alpha_dash>>)'),
('type_name', r'#(<<type_name:alpha>>)'),
('enum_value', r'%(<<member_name:alpha>>)'),
('parameter', r'@<<param_name:alpha>>'),
('function_call', r'<<symbol_name:alpha>>\(\)'),
]
super(DocstringScanner, self).__init__(specs)
class DocFormatter(object):
def __init__(self, transformer):
self._transformer = transformer
self._scanner = DocstringScanner()
# If we are processing a code block as defined by
# https://wiki.gnome.org/Projects/GTK%2B/DocumentationSyntax/Markdown
# we won't insert paragraphs and will respect new lines.
self._processing_code = False
self._processing_attr = False
def escape(self, text):
return saxutils.escape(text)
def unescape(self, text):
return saxutils.unescape(text)
def should_render_node(self, node):
if getattr(node, "private", False):
return False
# Nodes without namespace are AST bugs really
# They are used for structs and unions declared
# inline inside other structs, but they are not
# even picked up by g-ir-compiler, because they
# don't create a <type/> element.
# So just ignore them.
if isinstance(node, ast.Node) and node.namespace is None:
return False
return True
def format(self, node, doc):
if doc is None:
return ''
result = ''
for para in doc.split('\n\n'):
result += ' <p>'
result += self.format_inline(node, para)
result += '</p>'
return result
def _resolve_type(self, ident):
try:
matches = self._transformer.split_ctype_namespaces(ident)
except ValueError:
return None
for namespace, name in matches:
node = namespace.get(name)
if node:
return node
return None
def _resolve_symbol(self, symbol):
try:
matches = self._transformer.split_csymbol_namespaces(symbol)
except ValueError:
return None
for namespace, name in matches:
node = namespace.get_by_symbol(symbol)
if node:
return node
return None
def _find_thing(self, list_, name):
for item in list_:
if item.name == name:
return item
raise KeyError("Could not find %s" % (name, ))
def _process_other(self, node, match, props):
if self._processing_code:
return match
return self.escape(match)
def _process_property(self, node, match, props):
type_node = self._resolve_type(props['type_name'])
if type_node is None:
return match
try:
prop = self._find_thing(type_node.properties, props['property_name'])
except (AttributeError, KeyError):
return match
return self.format_xref(prop)
def _process_signal(self, node, match, props):
type_node = self._resolve_type(props['type_name'])
if type_node is None:
return match
try:
signal = self._find_thing(type_node.signals, props['signal_name'])
except (AttributeError, KeyError):
return match
return self.format_xref(signal)
def _process_type_name(self, node, match, props):
if self._processing_attr:
return match
ident = props['type_name']
type_ = self._resolve_type(ident)
plural = False
if type_ is None:
singularized = ident.rstrip("s") # Try to remove plural
type_ = self._resolve_type(singularized)
plural = True
if type_ is None:
return match
return self.format_xref(type_, pluralize=plural)
def _process_enum_value(self, node, match, props):
member_name = props['member_name']
try:
return '<code>%s</code>' % (self.fundamentals[member_name], )
except KeyError:
pass
enum_value = self._resolve_symbol(member_name)
if enum_value:
return self.format_xref(enum_value)
return match
def _process_parameter(self, node, match, props):
try:
parameter = node.get_parameter(props['param_name'])
except (AttributeError, ValueError):
return match
return '<code>%s</code>' % (self.format_parameter_name(node, parameter), )
def _process_function_call(self, node, match, props):
func = self._resolve_symbol(props['symbol_name'])
if func is None:
return match
return self.format_xref(func)
# FIXME: the four spaces after newlines in the following functions are to
# keep Markdown happy. We pass the documentation string first through this
# templated scanner, which converts |[ ]| to <pre></pre>. Then in the case
# of DevDocs output, we pass the resulting string through Markdown; but
# Markdown will not respect the <pre> element and will treat the code as
# markup, converting asterisks into <em> etc. Putting four spaces at the
# start of each line makes Markdown recognize the code as code without
# affecting the normal HTML output too much.
#
# A better solution would be to replace DocstringScanner by Markdown
# entirely, implementing the custom markup with Markdown extensions.
#
# UPDATE: As a temporary fix for code blocks we will convert directly to ``` syntax.
#
# NOTES:
# _process_markdown_code_toggle:
# Whenever we encounter ` we need to toggle whether we are escaping text as text inside
# inline code blocks is unescaped
# _process_markdown_attr_(start|end):
# Whenever we encounter { or } we must stop parsing type names as curly braces are used for
# attributes in GIR files in addition to type declarations.
# _process_html_code_(start|end):
# Whenever we encounter an HTML <code> block we must stop escaping text.
#
# TODO: Convert to markdown extensions.
def _process_markdown_code_toggle(self, node, match, props):
self._processing_code = not self._processing_code
return match
def _process_markdown_attr_start(self, node, match, props):
if not self._processing_code:
self._processing_attr = True
return match
def _process_markdown_attr_end(self, node, match, props):
if not self._processing_code:
self._processing_attr = False
return match
def _process_html_code_start(self, node, match, props):
self._processing_code = True
return match
def _process_html_code_end(self, node, match, props):
self._processing_code = False
return match
def _process_code_start(self, node, match, props):
self._processing_code = True
return '</p>\n```\n'
def _process_code_start_with_language(self, node, match, props):
self._processing_code = True
try:
return '</p>\n```' + props["language_name"].lower() + '\n'
except KeyError:
return '</p>\n```\n'
def _process_code_end(self, node, match, props):
self._processing_code = False
return '\n```\n<p>'
def _process_new_line(self, node, match, props):
if self._processing_code:
return '\n'
return '\n'
def _process_new_paragraph(self, node, match, props):
if self._processing_code:
return '\n\n'
return "</p><p>"
def _process_token(self, node, tok):
kind, match, props = tok
dispatch = {
'other': self._process_other,
'property': self._process_property,
'signal': self._process_signal,
'type_name': self._process_type_name,
'enum_value': self._process_enum_value,
'parameter': self._process_parameter,
'function_call': self._process_function_call,
'code_start': self._process_code_start,
'code_start_with_language': self._process_code_start_with_language,
'code_end': self._process_code_end,
'html_code_start': self._process_html_code_start,
'html_code_end': self._process_html_code_end,
'markdown_code_toggle': self._process_markdown_code_toggle,
'markdown_attr_start': self._process_markdown_attr_start,
'markdown_attr_end': self._process_markdown_attr_end,
'new_line': self._process_new_line,
'new_paragraph': self._process_new_paragraph,
}
return dispatch[kind](node, match, props)
def get_in_parameters(self, node):
raise NotImplementedError
def format_inline(self, node, para):
tokens = self._scanner.scan(para)
words = [self._process_token(node, tok) for tok in tokens]
return ''.join(words)
def format_parameter_name(self, node, parameter):
if isinstance(parameter.type, ast.Varargs):
return "..."
else:
return parameter.argname
def format_function_name(self, func):
raise NotImplementedError
def format_type(self, type_, link=False):
raise NotImplementedError
def format_value(self, node):
raise NotImplementedError
def format_page_name(self, node):
if isinstance(node, ast.Namespace):
return node.name
elif isinstance(node, ast.Function):
return self.format_function_name(node)
elif isinstance(node, ast.Property) and node.parent is not None:
return '%s:%s' % (self.format_page_name(node.parent), node.name)
elif isinstance(node, ast.Signal) and node.parent is not None:
return '%s::%s' % (self.format_page_name(node.parent), node.name)
elif isinstance(node, ast.VFunction) and node.parent is not None:
return '%s::%s' % (self.format_page_name(node.parent), node.name)
elif isinstance(node, ast.Field) and node.parent is not None:
return '%s->%s' % (self.format_page_name(node.parent), node.name)
else:
return make_page_id(node)
def format_xref(self, node, pluralize=False, **attrdict):
if node is None or not hasattr(node, 'namespace'):
attrs = [('xref', 'index')] + list(sorted(attrdict.items()))
return xmlwriter.build_xml_tag('link', attrs)
elif isinstance(node, ast.Member):
# Enum/BitField members are linked to the main enum page.
return self.format_xref(node.parent, pluralize=pluralize, **attrdict) + '.' + node.name
elif node.namespace is self._transformer.namespace:
return self.format_internal_xref(node, attrdict, pluralize=pluralize)
else:
return self.format_external_xref(node, attrdict, pluralize=pluralize)
def format_internal_xref(self, node, attrdict, pluralize=False):
attrs = [('xref', make_page_id(node))] + list(sorted(attrdict.items()))
if not pluralize:
return xmlwriter.build_xml_tag('link', attrs)
else:
return xmlwriter.build_xml_tag('link', attrs, make_page_id(node) +
"s")
def format_external_xref(self, node, attrdict, pluralize=False):
ns = node.namespace
attrs = [('href', '../%s-%s/%s.html' % (ns.name, str(ns.version),
make_page_id(node)))]
attrs += list(sorted(attrdict.items()))
if not pluralize:
return xmlwriter.build_xml_tag('link', attrs, self.format_page_name(node))
else:
return xmlwriter.build_xml_tag('link', attrs,
self.format_page_name(node) + "s")
def field_is_writable(self, field):
return True
def format_property_flags(self, property_, construct_only=False, abbrev=False):
flags = []
if property_.readable and not construct_only:
flags.append("Read")
if property_.writable and not construct_only and \
self.field_is_writable(property_):
flags.append("Write")
if isinstance(property_, ast.Property):
if property_.construct:
flags.append("Construct")
if property_.construct_only:
flags.append("Construct Only")
if abbrev:
return "/".join([''.join([word[0] for word in flag.lower().split()])
for flag in flags])
return " / ".join(flags)
def format_signal_flags(self, signal):
flags = []
if signal.action:
flags.append("Action")
if signal.detailed:
flags.append("Detailed")
if signal.no_hooks:
flags.append("No Hooks")
if signal.no_recurse:
flags.append("No Recurse")
if signal.when:
flags.append("Run " + signal.when.capitalize())
return " / ".join(flags)
def to_underscores(self, node):
if isinstance(node, ast.Property):
return node.name.replace('-', '_')
elif node.name:
return to_underscores(node.name)
elif isinstance(node, ast.Function) and node.moved_to:
return to_underscores(node.moved_to)
elif isinstance(node, ast.Callback):
return 'callback'
elif isinstance(node, ast.Union):
return 'anonymous_union'
elif isinstance(node, ast.Field):
return 'anonymous field'
else:
raise Exception('invalid node')
def to_lower_camel_case(self, string):
return string[0].lower() + string[1:]
def get_class_hierarchy(self, node):
assert isinstance(node, ast.Class)
parent_chain = [node]
while node.parent_type:
node = self._transformer.lookup_typenode(node.parent_type)
parent_chain.append(node)
parent_chain.reverse()
return parent_chain
def get_inheritable_types(self, node):
"""Return an ast.Node object for each type (ast.Class and ast.Interface
types) from which an ast.Class @node might inherit methods, properties,
and signals."""
assert isinstance(node, ast.Class)
parent_chain = self.get_class_hierarchy(node)
types = []
for p in parent_chain:
types += [self._transformer.lookup_typenode(t) for t in p.interfaces]
types += [t for t in parent_chain if t is not node]
return types
def is_private_field(self, node, f):
"""Returns whether @f is a private field of @node (including a heuristic
that tries to determine whether the field is the parent instance field
or a private pointer but not marked as such.)"""
if f.private:
return True
if f.anonymous_node:
return True
if f.name == 'g_type_instance':
return True # this field on GObject is not exposed
field_typenode = self._transformer.lookup_typenode(f.type)
if not field_typenode:
return False
if getattr(field_typenode, 'disguised', False):
return True # guess that it's a pointer to a private struct
# this also catches fields of type GdkAtom, since that is disguised
# as well. Not sure whether that's correct or not.
if not isinstance(node, ast.Class):
return False # parent instance heuristics only apply to classes
if node.parent_type:
parent_typenode = self._transformer.lookup_typenode(node.parent_type)
if field_typenode == parent_typenode:
return True # guess that it's a parent instance field
return False
def format_prerequisites(self, node):
assert isinstance(node, ast.Interface)
if len(node.prerequisites) > 0:
if len(node.prerequisites) > 1:
return ', '.join(node.prerequisites[:-1]) + \
' and ' + node.prerequisites[-1]
else:
return node.prerequisites[0]
else:
return 'GObject.Object'
def format_known_implementations(self, node):
assert isinstance(node, ast.Interface)
node_name = node.namespace.name + '.' + node.name
impl = []
for c in node.namespace.values():
if not isinstance(c, ast.Class):
continue
for implemented in c.interfaces:
if implemented.target_giname == node_name:
impl.append(c)
break
if len(impl) == 0:
return 'None'
else:
out = '%s is implemented by ' % (node.name,)
if len(impl) == 1:
return out + impl[0].name
else:
return out + ', '.join(i.name for i in impl[:-1]) + \
' and ' + impl[-1].name
class DocFormatterC(DocFormatter):
language = "C"
mime_type = "text/x-csrc"
output_format = "mallard"
output_extension = ".page"
fundamentals = {
"TRUE": "TRUE",
"FALSE": "FALSE",
"NULL": "NULL",
}
def format_type(self, type_, link=False):
if isinstance(type_, ast.Array):
return self.format_type(type_.element_type) + '*'
elif type_.ctype is not None:
return type_.ctype
elif type_.target_fundamental:
return type_.target_fundamental
else:
node = self._transformer.lookup_typenode(type_)
return getattr(node, 'ctype')
def format_function_name(self, func):
if isinstance(func, ast.Function):
return func.symbol
else:
return func.name
def get_in_parameters(self, node):
return node.all_parameters
class DocFormatterIntrospectableBase(DocFormatter):
def should_render_node(self, node):
if isinstance(node, ast.Record) and node.is_gtype_struct_for is not None:
return False
if not getattr(node, "introspectable", True):
return False
if isinstance(node, ast.Function) and node.shadowed_by is not None:
return False
return super(DocFormatterIntrospectableBase, self).should_render_node(node)
class DocFormatterPython(DocFormatterIntrospectableBase):
language = "Python"
mime_type = "text/python"
output_format = "mallard"
output_extension = ".page"
fundamentals = {
"TRUE": "True",
"FALSE": "False",
"NULL": "None",
}
def get_output_format(self):
return 'mallard'
def get_output_extension(self):
return 'page'
def should_render_node(self, node):
if getattr(node, "is_constructor", False):
return False
return super(DocFormatterPython, self).should_render_node(node)
def is_method(self, node):
if getattr(node, "is_method", False):
return True
if isinstance(node, ast.VFunction):
return True
return False
def format_parameter_name(self, node, parameter):
# Force "self" for the first parameter of a method
if self.is_method(node) and parameter is node.instance_parameter:
return "self"
elif isinstance(parameter.type, ast.Varargs):
return "..."
else:
return parameter.argname
def format_fundamental_type(self, name):
fundamental_types = {
"utf8": "unicode",
"gunichar": "unicode",
"gchar": "str",
"guchar": "str",
"gboolean": "bool",
"gint": "int",
"guint": "int",
"glong": "int",
"gulong": "int",
"gint64": "int",
"guint64": "int",
"gfloat": "float",
"gdouble": "float",
"gchararray": "str",
"GParam": "GLib.Param",
"PyObject": "object",
"GStrv": "[str]",
"GVariant": "GLib.Variant"}
return fundamental_types.get(name, name)
def format_type(self, type_, link=False):
if isinstance(type_, (ast.List, ast.Array)):
return '[' + self.format_type(type_.element_type) + ']'
elif isinstance(type_, ast.Map):
return '{%s: %s}' % (self.format_type(type_.key_type),
self.format_type(type_.value_type))
elif type_.target_giname is not None:
return type_.target_giname
else:
return self.format_fundamental_type(type_.target_fundamental)
def format_function_name(self, func):
if func.parent is not None:
return "%s.%s" % (self.format_page_name(func.parent), func.name)
else:
return func.name
def get_in_parameters(self, node):
return node.all_parameters
class DocFormatterGjs(DocFormatterIntrospectableBase):
language = "Gjs"
mime_type = "text/x-gjs"
output_format = "mallard"
output_extension = ".page"
fundamentals = {
"TRUE": "true",
"FALSE": "false",
"NULL": "null",
}
def is_method(self, node):
if getattr(node, "is_method", False):
return True
if isinstance(node, ast.VFunction):
return True
return False
def resolve_gboxed_constructor(self, node):
zero_args_constructor = None
default_constructor = None
introspectable_constructors = \
list(filter(lambda c: getattr(c, 'introspectable', True),
node.constructors))
for c in introspectable_constructors:
if zero_args_constructor is None and \
len(c.parameters) == 0:
zero_args_constructor = c
if default_constructor is None and \
c.name == 'new':
default_constructor = c
if default_constructor is None:
default_constructor = zero_args_constructor
if default_constructor is None and \
len(introspectable_constructors) > 0:
default_constructor = introspectable_constructors[0]
node.gjs_default_constructor = default_constructor
node.gjs_zero_args_constructor = zero_args_constructor
def should_render_node(self, node):
if isinstance(node, (ast.Compound, ast.Boxed)):
self.resolve_gboxed_constructor(node)
if isinstance(node, ast.Compound) and node.disguised and \
len(node.methods) == len(node.static_methods) == len(node.constructors) == 0:
return False
if isinstance(node, ast.ErrorQuarkFunction):
return False
if isinstance(node, ast.Field):
if node.type is None:
return False
if isinstance(node.parent, (ast.Class, ast.Union)):
return False
if isinstance(node, ast.Union) and node.name is None:
return False
if isinstance(node, ast.Class):
is_gparam_subclass = False
if node.parent_type:
parent = self._transformer.lookup_typenode(node.parent_type)
while parent:
if parent.namespace.name == 'GObject' and \
parent.name == 'ParamSpec':
is_gparam_subclass = True
break
if parent.parent_type is None:
break
parent = self._transformer.lookup_typenode(parent.parent_type)
if is_gparam_subclass:
return False
return super(DocFormatterGjs, self).should_render_node(node)
def format_fundamental_type(self, name):
fundamental_types = {
"none": "void",
"gpointer": "void",
"gboolean": "Boolean",
"gint8": "Number(gint8)",
"guint8": "Number(guint8)",
"gint16": "Number(gint16)",
"guint16": "Number(guint16)",
"gint32": "Number(gint32)",
"guint32": "Number(guint32)",
"gchar": "Number(gchar)",
"guchar": "Number(guchar)",
"gshort": "Number(gshort)",
"gint": "Number(gint)",
"guint": "Number(guint)",
"gfloat": "Number(gfloat)",
"gdouble": "Number(gdouble)",
"utf8": "String",
"gunichar": "String",
"filename": "String",
"GType": "GObject.Type",
"GVariant": "GLib.Variant",
# These cannot be fully represented in gjs
"gsize": "Number(gsize)",
"gssize": "Number(gssize)",
"gintptr": "Number(gintptr)",
"guintptr": "Number(guintptr)",
"glong": "Number(glong)",
"gulong": "Number(gulong)",
"gint64": "Number(gint64)",
"guint64": "Number(guint64)",
"long double": "Number(long double)",
"long long": "Number(long long)",
"unsigned long long": "Number(unsigned long long)"}
return fundamental_types.get(name, name)
def format_type(self, type_, link=False):
if isinstance(type_, ast.Array) and \
type_.element_type.target_fundamental in ('gint8', 'guint8'):
return 'ByteArray'
elif isinstance(type_, (ast.List, ast.Array)):
return 'Array(' + self.format_type(type_.element_type, link) + ')'
elif isinstance(type_, ast.Map):
return '{%s: %s}' % (self.format_type(type_.key_type, link),
self.format_type(type_.value_type, link))
elif not type_ or type_.target_fundamental == "none":
return "void"
elif type_.target_giname is not None:
giname = type_.target_giname
if giname == 'Gdk.Atom':
return 'String'
if giname in ('GLib.ByteArray', 'GLib.Bytes'):
return 'ByteArray'
if giname == 'GObject.Value':
return 'Any'
if giname == 'GObject.Closure':
return 'Function'
if link:
nsname = self._transformer.namespace.name
if giname.startswith(nsname + '.'):
return '<link xref="%s">%s</link>' % (giname, giname)
else:
resolved = self._transformer.lookup_typenode(type_)
if resolved:
return self.format_xref(resolved)
return giname
else:
return self.format_fundamental_type(type_.target_fundamental)
def format_function_name(self, func):
name = func.name
if func.shadows:
name = func.shadows
if func.is_method:
return "%s.prototype.%s" % (self.format_page_name(func.parent), name)
elif func.parent is not None:
return "%s.%s" % (self.format_page_name(func.parent), name)
else:
return name
def format_page_name(self, node):
if isinstance(node, (ast.Field, ast.Property)):
return '%s.%s' % (self.format_page_name(node.parent), self.to_underscores(node))
else:
return DocFormatterIntrospectableBase.format_page_name(self, node)
def has_any_parameters(self, node):
return len(node.parameters) > 0 or \
node.retval.type.target_fundamental != 'none'
def get_in_parameters(self, node):
skip = set()
for param in node.parameters:
if param.direction == ast.PARAM_DIRECTION_OUT:
skip.add(param)
if param.closure_name is not None:
skip.add(node.get_parameter(param.closure_name))
if param.destroy_name is not None:
skip.add(node.get_parameter(param.destroy_name))
if isinstance(param.type, ast.Array) and param.type.length_param_name is not None:
skip.add(node.get_parameter(param.type.length_param_name))
params = []
for param in node.parameters:
if param not in skip:
params.append(param)
return params
def get_out_parameters(self, node):
skip = set()
for param in node.parameters:
if param.direction == ast.PARAM_DIRECTION_IN:
skip.add(param)
if param.closure_name is not None:
skip.add(node.get_parameter(param.closure_name))
if param.destroy_name is not None:
skip.add(node.get_parameter(param.destroy_name))
if isinstance(param.type, ast.Array) and param.type.length_param_name is not None:
skip.add(node.get_parameter(param.type.length_param_name))
params = []
if node.retval.type.target_fundamental != 'none':
name = 'return_value'
if node.retval.type.target_fundamental == 'gboolean':
name = 'ok'
ret_param = ast.Parameter(name, node.retval.type,
ast.PARAM_DIRECTION_OUT)
ret_param.doc = node.retval.doc
params.append(ret_param)
for param in node.parameters:
if param not in skip:
params.append(param)
if len(params) == 1:
params[0].argname = 'Returns'
return params
def format_in_parameters(self, node):
in_params = self.get_in_parameters(node)
return ', '.join(('%s: %s' % (p.argname, self.format_type(p.type, True)))
for p in in_params)
def format_out_parameters(self, node):
out_params = self.get_out_parameters(node)
if len(out_params) == 0:
return 'void'
elif len(out_params) == 1:
return self.format_type(out_params[0].type, True)
else:
return '[' + ', '.join(('%s: %s' % (p.argname, self.format_type(p.type, True)))
for p in out_params) + ']'
def field_is_writable(self, node):
if isinstance(node, ast.Field):
if node.type is None:
return False
if node.private:
return False
if isinstance(node.parent, ast.Union):
return False
if node.type.target_fundamental not in \
(None, 'none', 'gpointer', 'utf8', 'filename', 'va_list'):
return True
resolved = self._transformer.lookup_typenode(node.type)
if resolved:
if isinstance(resolved, ast.Compound) and node.type.ctype[-1] != '*':
return self._struct_is_simple(resolved)
elif isinstance(resolved, (ast.Enum, ast.Bitfield)):
return True
return False
else:
return True
def _struct_is_simple(self, node):
if node.disguised or len(node.fields) == 0:
return False
for f in node.fields:
if not self.field_is_writable(f):
return False
return True
def format_gboxed_constructor(self, node):
if node.namespace.name == 'GLib' and node.name == 'Variant':
return 'signature: String, value: Any'
zero_args_constructor = node.gjs_zero_args_constructor
default_constructor = node.gjs_default_constructor
can_allocate = zero_args_constructor is not None
if not can_allocate and isinstance(node, ast.Record):
can_allocate = self._struct_is_simple(node)
# Small lie: if can_allocate is False, and
# default_constructor is None, then you cannot
# construct the boxed in any way. But let's
# pretend you can with the regular constructor
if can_allocate or default_constructor is None:
if isinstance(node, ast.Compound):
fields = filter(self.field_is_writable, node.fields)
out = ''
for f in fields:
out += " <link xref='%s.%s-%s'>%s</link>: value\n" % \
(node.namespace.name, node.name, f.name, f.name)
if out:
out = "{\n" + out + "}"
return out
else:
return ''
else:
construct_params = self.get_in_parameters(default_constructor)
return ', '.join(('%s: %s' % (p.argname, self.format_type(p.type)))
for p in construct_params)
class DevDocsFormatterGjs(DocFormatterGjs):
output_format = "devdocs"
output_extension = ".html"
def _is_static_method(self, node):
if not hasattr(node.parent, "static_methods"):
return False
return node in node.parent.static_methods
def should_render_node(self, node):
# For DevDocs, we only want to render the top-level nodes.
if isinstance(node, (ast.Compound, ast.Boxed)):
self.resolve_gboxed_constructor(node)
if not super(DevDocsFormatterGjs, self).should_render_node(node):
return False
if isinstance(node, ast.Function) and not node.is_method and \
not node.is_constructor and not self._is_static_method(node):
return True # module-level function
toplevel_types = [ast.Alias, ast.Bitfield, ast.Boxed, ast.Callback,
ast.Class, ast.Constant, ast.Enum, ast.Interface, ast.Namespace,
ast.Record, ast.Union]
for ast_type in toplevel_types:
if isinstance(node, ast_type):
return True
return False
def format_fundamental_type(self, name):
# Don't specify the C type after Number as the Mallard docs do; it's
# confusing to GJS newbies.
if name in ["gint8", "guint8", "gint16", "guint16", "gint32", "guint32",
"gchar", "guchar", "gshort", "gint", "guint", "gfloat",
"gdouble", "gsize", "gssize", "gintptr", "guintptr",
"glong", "gulong", "gint64", "guint64", "long double",
"long long", "unsigned long long"]:
return "Number" # gsize and up cannot fully be represented in GJS
if name in ["none", "gpointer"]:
return "void"
if name in ["utf8", "gunichar", "filename"]:
return "String"
if name == "gboolean":
return "Boolean"
if name == "GType":
return "GObject.Type"
if name == "GVariant":
return "GLib.Variant"
return name
def format_value(self, node):
# Constants only have fundamental types?
type_ = node.value_type.target_fundamental
if type_ in ["utf8", "gunichar", "filename"]:
return repr(node.value)
# escapes quotes in the string; ought to be the same in Javascript
return node.value
def format(self, node, doc):
if doc is None:
return ''
cleaned_up_gtkdoc = super(DevDocsFormatterGjs, self).format_inline(node, doc)
return markdown.markdown(cleaned_up_gtkdoc, extensions=[
'markdown.extensions.fenced_code',
'markdown.extensions.nl2br',
'markdown.extensions.attr_list',
HeaderIdExtension(forceid=False)
])
def format_function_name(self, func):
name = func.name
if func.shadows:
name = func.shadows
if isinstance(func, ast.VFunction):
return 'vfunc_' + name
return name
def format_page_name(self, node):
if isinstance(node, ast.Function) and node.parent is not None:
return node.parent.name + "." + self.format_function_name(node)
return super(DevDocsFormatterGjs, self).format_page_name(node)
def _write_xref_markdown(self, target, anchor=None, display_name=None, pluralize=False):
if display_name is None:
display_name = target
link = target + ".html"
if anchor is not None:
link += "#" + anchor
return "[{}]({}){}".format(display_name, link, 's' if pluralize else '')
def to_underscores(self, node):
try:
return super(DevDocsFormatterGjs, self).to_underscores(node)
except Exception as e:
if e.message == 'invalid node':
print('warning: invalid node in', node.parent.name,
file=sys.stderr)
return node.parent.name + '_invalid_node'
def make_anchor(self, node):
style_class = get_node_kind(node)
return "{}-{}".format(style_class, self.to_underscores(node))
def _process_parameter(self, node, match, props):
# Display the instance parameter as "this" instead of whatever name it
# has in C.
if hasattr(node, 'instance_parameter') and \
node.instance_parameter is not None and \
props['param_name'] == node.instance_parameter.argname:
return '<code>this</code>'
return super(DevDocsFormatterGjs, self)._process_parameter(node, match, props)
def format_xref(self, node, pluralize=False, **attrdict):
if node is None or not hasattr(node, 'namespace'):
return self._write_xref_markdown('index')
if node.namespace is self._transformer.namespace:
return self.format_internal_xref(node, attrdict, pluralize=pluralize)
return self.format_external_xref(node, attrdict, pluralize=pluralize)
def format_internal_xref(self, node, attrdict, pluralize=False):
if not self.should_render_node(node):
# Non-toplevel nodes are linked to the main page.
page = make_page_id(node.parent)
name = node.name
if isinstance(node, ast.Member):
name = name.upper()
return self._write_xref_markdown(page, self.make_anchor(node),
page + "." + name,
pluralize=pluralize)
return self._write_xref_markdown(make_page_id(node), pluralize=pluralize)
def format_external_xref(self, node, attrdict, pluralize=False):
ns = node.namespace
slug = ns.name.lower() + str(ns.version).replace('.', '')
if not self.should_render_node(node):
target = 'gir:///%s/%s' % (slug, make_page_id(node.parent))
return self._write_xref_markdown(target, self.make_anchor(node),
self.format_page_name(node.parent),
pluralize=pluralize)
target = 'gir:///%s/%s' % (slug, make_page_id(node))
return self._write_xref_markdown(target, None,
self.format_page_name(node),
pluralize=pluralize)
def format_inline(self, node, para):
if para is None:
return ''
cleaned_up_gtkdoc = super(DevDocsFormatterGjs, self).format_inline(node, para)
return markdown.markdown(cleaned_up_gtkdoc, extensions=[
InlineMarkdown(),
'markdown.extensions.fenced_code',
'markdown.extensions.nl2br',
'markdown.extensions.attr_list',
HeaderIdExtension(forceid=False)
])
def format_in_parameters(self, node):
return ', '.join(p.argname for p in self.get_in_parameters(node))
LANGUAGES = {
"devdocs": {
"gjs": DevDocsFormatterGjs,
},
"mallard": {
"c": DocFormatterC,
"python": DocFormatterPython,
"gjs": DocFormatterGjs,
},
}
class DocWriter(object):
def __init__(self, transformer, language, output_format):
self._transformer = transformer
try:
formatter_class = LANGUAGES[output_format][language.lower()]
except KeyError:
raise SystemExit("Unsupported language %s for output format %s" %
(language, output_format))
self._formatter = formatter_class(self._transformer)
self._language = self._formatter.language
self._output_format = output_format
self._lookup = self._get_template_lookup()
def _get_template_lookup(self):
if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
srcdir = os.path.join(top_srcdir, 'giscanner')
else:
srcdir = os.path.dirname(__file__)
template_dir = os.path.join(srcdir, 'doctemplates',
self._formatter.output_format)
return TemplateLookup(directories=[template_dir],
module_directory=tempfile.mkdtemp(),
output_encoding='utf-8')
def write(self, output):
try:
os.makedirs(output)
except OSError:
# directory already made
pass
self._walk_node(output, self._transformer.namespace, [])
self._transformer.namespace.walk(lambda node, chain: self._walk_node(output, node, chain))
def _walk_node(self, output, node, chain):
if isinstance(node, ast.Function) and node.moved_to is not None:
return False
if self._formatter.should_render_node(node):
self._render_node(node, chain, output)
# hack: fields are not Nodes in the ast, so we don't
# see them in the visit. Handle them manually here
if isinstance(node, (ast.Compound, ast.Class)):
chain.append(node)
for f in node.fields:
self._walk_node(output, f, chain)
chain.pop()
return True
return False
def _render_node(self, node, chain, output):
namespace = self._transformer.namespace
# A bit of a hack...maybe this should be an official API
node._chain = list(chain)
page_kind = get_node_kind(node)
template_name = '%s/%s.tmpl' % (self._language, page_kind)
page_id = make_page_id(node)
template = self._lookup.get_template(template_name)
result = template.render(namespace=namespace,
node=node,
page_id=page_id,
page_kind=page_kind,
get_node_kind=get_node_kind,
formatter=self._formatter,
ast=ast)
output_base_name = page_id + self._formatter.output_extension
output_file_name = os.path.join(os.path.abspath(output),
output_base_name)
with open(output_file_name, 'wb') as fp:
fp.write(result)
|