-
Notifications
You must be signed in to change notification settings - Fork 2
/
unreal_engine_docset.py
1068 lines (859 loc) · 27.7 KB
/
unreal_engine_docset.py
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
"""
Unreal Engine Docset
====================
Defines the objects to generate a `Dash <https://kapeli.com/dash>`__ compatible
docset from `Unreal Engine documentation <https://docs.unrealengine.com>`__:
- :func:`generate_docset`
"""
from __future__ import annotations
import html
import logging
import multiprocessing
import os
import re
import shutil
import sqlite3
import tempfile
import time
import xml.etree.ElementTree as Et
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
from typing import Callable, TypedDict, cast
from xml.dom import minidom
import click
import lxml.html
import setuptools.archive_util
from lxml import etree # pyright: ignore
from lxml.html import fromstring, tostring
from tqdm.contrib.concurrent import process_map
from typing_extensions import Unpack
__author__ = "Thomas Mansencal"
__copyright__ = "Copyright 2024 Thomas Mansencal"
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Thomas Mansencal"
__email__ = "thomas.mansencal@gmail.com"
__status__ = "Production"
__all__ = [
"Collector",
"ApiInformation",
"Entry",
"MAPPING_API_TYPE_TO_ENTRY_TYPE",
"chdir",
"join_path",
"read_xml_file",
"collector_cpp_default",
"collector_cpp_nohref",
"COLLECTORS_CPP",
"collector_blueprint_default",
"COLLECTORS_BLUEPRINT",
"collect_api_name_and_syntax",
"collect_api_information",
"process_cpp_html_file",
"KwargsDocsetProcessor",
"process_cpp_docset",
"process_blueprint_html_file",
"process_blueprint_docset",
"process_python_docset",
"generate_database",
"generate_plist",
"generate_docset",
]
logger = logging.getLogger(__name__)
@dataclass
class Collector:
"""
Define a collector selecting data from *Unreal Engine* documentation.
Parameters
----------
type
Entry type corresponding to a supported *Dash*
`entry type <https://kapeli.com/docsets#supportedentrytypes>`__.
predicate
*XPath* expression predicate to collect the data.
processor
Callable processing the collected data.
"""
type: str # noqa: A003
predicate: str
processor: Callable
@dataclass(frozen=True)
class ApiInformation:
"""
Define the *API* information for an *Unreal Engine* object and *Dash*
documentation.
Parameters
----------
name
Object *API* name.
ue_type
*Unreal Engine* object *API* type, e.g. ``UCLASS``.
dash_type
*Dash* documentation entry type, e.g. ``Class``.
"""
name: str | None
ue_type: str | None
dash_type: str | None
@dataclass(frozen=True)
class Entry:
"""
Define a *Dash* documentation entry.
Parameters
----------
name
Object name.
path
Object path in the documentation, typically a relative html path.
type
*Dash* entry type, e.g. ``Class``.
"""
name: str
path: str
type: str # noqa: A003
MAPPING_API_TYPE_TO_ENTRY_TYPE: dict = {
"class": "Class",
"UCLASS": "Class",
"struct": "Struct",
"USTRUCT": "Struct",
"union": "Union",
}
"""Mapping of *Unreal Engine* *API* type to *Dash* entry type."""
class chdir:
"""
Define a context manager to change the current working directory.
Parameters
----------
path
Desired working directory.
"""
def __init__(self, path: Path | str):
self.path = path
self._old_cwd = []
def __enter__(self):
"""Set the current working directory."""
self._old_cwd.append(os.getcwd())
os.chdir(self.path)
def __exit__(self, *excinfo):
"""Restore the current working directory."""
os.chdir(self._old_cwd.pop())
def join_path(parent: Path | str, name: str) -> str:
"""
Join given parent and name into a path.
Parameters
----------
parent
Parent to join.
name
Name to join.
Returns
-------
:class:`str`
Joined path.
"""
parent = re.sub(r"\\?/?index.html$", "", str(parent))
return f"{parent}/{name}"
def read_xml_file(xml_path: Path | str, attempts: int = 10) -> lxml.html.HtmlElement:
"""
Attempt to read given *XML* file.
Because of multiprocessing, it seems like on *macOS*, files might be
accessed concurrently causing *XML* parsing failure.
Parameters
----------
xml_path
Path of the *XML* file to read.
attempts
Number of attempts to try to read the file. One is usually required.
Returns
-------
:class:`lxml.html.HtmlElement`
*XML* element.
"""
i = 0
while i < attempts:
try:
with open(xml_path) as xml_file:
xml = xml_file.read()
return fromstring(xml.encode("utf-8"))
except etree.ParserError:
logger.debug(
'Could not parse "%s" file on attempt %s, sleeping...', xml_path, i + 1
)
i += 1
time.sleep(0.1)
continue
raise RuntimeError('Could not parse "%s" file!', xml_path)
def collector_cpp_default(
elements: list[lxml.html.HtmlElement],
parent_api_information: ApiInformation, # noqa: ARG001
html_path: Path,
) -> list[tuple[str, str]]:
"""
Collect the *C++* *API* data for given elements.
This is the default collector.
Parameters
----------
elements
Elements to collect the data from.
parent_api_information
Parent *Unreal Engine* object and *Dash* documentation *API*
information.
html_path
Path of the file the elements are contained into.
Returns
-------
:class:`list`
Collected data.
"""
collection = []
for element in elements:
collected_path = join_path(html_path, element.attrib["href"])
if not Path(collected_path).exists():
continue
collected_name = collect_api_information(read_xml_file(collected_path)).name
collection.append((collected_name, collected_path))
return collection
def collector_cpp_nohref(
elements: list[lxml.html.HtmlElement],
parent_api_information: ApiInformation,
html_path: Path,
) -> list[tuple[str, str]]:
"""
Collect the *C++* *API* data for given elements without an ``href``.
Parameters
----------
elements
Elements to collect the data from.
parent_api_information
Parent *Unreal Engine* object and *Dash* documentation *API*
information.
html_path
Path of the file the elements are contained into.
Returns
-------
:class:`list`
Collected data.
"""
collection = []
for element in elements:
name = element.text_content()
if parent_api_information.ue_type is not None:
name = f"{parent_api_information.name}.{name}"
collection.append((name, f"{html_path}#{name}"))
return collection
COLLECTORS_CPP: tuple[Collector, ...] = (
Collector(
"Module",
'.//div[@class="modules-list"]//td[@class="name-cell"]/a[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Class",
'.//div[@id="classes"]//td[@class="name-cell"][1]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Constructor",
'.//div[@id="constructor"]//td[@class="name-cell"][1]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Destructor",
'.//div[@id="destructor"]//td[@class="name-cell"][1]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Type",
'.//div[@id="typedefs"]//td[@class="name-cell"][1]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Enum",
'.//div[@id="enums"]//td[@class="name-cell"][1]/a[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Variable",
'.//div[@id="variables"]//td[@class="name-cell"][2]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Variable",
'.//div[@id="deprecatedvariables"]//td[@class="name-cell"][2]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Variable",
'.//div[@id="variables"]//td[@class="name-cell"][2]/p',
collector_cpp_nohref,
),
Collector(
"Variable",
'.//div[@id="deprecatedvariables"]//td[@class="name-cell"][2]/p',
collector_cpp_nohref,
),
Collector(
"Constant",
'.//div[@id="constants"]//td[@class="name-cell"][1]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Function",
'.//div[starts-with(@id, "functions_")]//td[@class="name-cell"][2]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
Collector(
"Function",
'.//div[starts-with(@id, "deprecatedfunctions")]//td[@class="name-cell"][2]/a'
'[not(@class="dashAnchor")]',
collector_cpp_default,
),
)
"""*Unreal Engine* documentation collectors for *C++*."""
def collector_blueprint_default(
elements: list[lxml.html.HtmlElement],
parent_api_information: ApiInformation,
html_path: Path,
) -> list[tuple[str, str]]:
"""
Collect the *Blueprint++* *API* data for given elements.
This is the default collector.
Parameters
----------
elements
Elements to collect the data from.
parent_api_information
Parent *Unreal Engine* object and *Dash* documentation *API*
information.
html_path
Path of the file the elements are contained into.
Returns
-------
:class:`list`
Collected data.
"""
collection = []
for element in elements:
collected_path = join_path(html_path, element.attrib["href"])
if not Path(collected_path).exists():
continue
collected_name = collect_api_information(read_xml_file(collected_path)).name
collection.append(
(f"{parent_api_information.name}.{collected_name}", collected_path)
)
return collection
COLLECTORS_BLUEPRINT: tuple[Collector, ...] = (
Collector(
"Function",
'.//h2[@id="actions"]/following-sibling::div[@class="member-list"]//td[@class="name-cell"]/a'
'[not(@class="dashAnchor")]',
collector_blueprint_default,
),
Collector(
"Category",
'.//h2[@id="categories"]/following-sibling::div[@class="member-list"]//td[@class="name-cell"]/a'
'[not(@class="dashAnchor")]',
collector_blueprint_default,
),
)
"""*Unreal Engine* documentation collectors for *Blueprint*."""
def collect_api_name_and_syntax(
xml: lxml.html.HtmlElement,
) -> tuple[str, str]:
"""
Collect the *API* name and syntax from given *XML* element.
Parameters
----------
xml
*XML* element to collect the *API* name and syntax from.
Returns
-------
:class:`tuple`
Collected *API* name and syntax.
"""
title = next(iter(xml.xpath('.//h1[@id="H1TitleId"]/text()'))).strip()
syntax = "\n".join(
element.text_content()
for element in xml.xpath('.//div[@class="simplecode_api"]/p')
)
return title, syntax
def collect_api_information(
xml: lxml.html.HtmlElement,
) -> ApiInformation:
"""
Collect the *API* information from given *XML* element.
Parameters
----------
xml
*XML* element to collect the *API* information from.
Returns
-------
:class:`ApiInformation`
Collected *API* information.
"""
api_name, api_syntax = collect_api_name_and_syntax(xml)
for api_type, entry_type in MAPPING_API_TYPE_TO_ENTRY_TYPE.items():
try:
search = re.search(f"{api_type} {api_name}", api_syntax, re.MULTILINE)
except re.error:
continue
if search is not None:
return ApiInformation(api_name, api_type, entry_type)
return ApiInformation(api_name, "object", "Object")
def process_cpp_html_file(
html_path: Path,
collectors: tuple = COLLECTORS_CPP,
add_dash_anchors: bool = True,
) -> list[Entry]:
"""
Process given *Unreal Engine* *C++* *HTML* file using given collectors.
Parameters
----------
html_path
*HTML* file path to collect the data from.
collectors
Collectors used to process the *HTML* file.
add_dash_anchors
Whether to add *Dash* anchors to generate a
`TOC <https://kapeli.com/docsets#tableofcontents>`__.
Returns
-------
:class:`list`
List of *Dash* documentation entries.
"""
logger.info('Processing "%s" file...', html_path)
def localiser(link):
"""Update given link to point to an actual *HTML* file"""
index_path = html_path.parent / Path(link) / "index.html"
if not index_path.exists():
return link
return f"{link}/index.html"
xml = read_xml_file(html_path)
xml.rewrite_links(localiser)
api_information = collect_api_information(xml)
has_dash_anchors = len(xml.xpath('.//a[@class="dashAnchor"]')) != 0
entries = []
for collector in collectors:
elements = xml.xpath(collector.predicate)
anchors = []
for i, collection in enumerate(
collector.processor(elements, api_information, html_path)
):
collected_name, collected_html_path = collection
if not Path(collected_html_path).exists():
continue
collected_type = collector.type
# "class", "UCLASS", "struct", "USTRUCT" and "union" are classified
# as "classes", we are providing more granularity.
if collector.type == "Class":
collected_xml = read_xml_file(collected_html_path)
collected_api_information = collect_api_information(collected_xml)
collected_type = collected_api_information.dash_type
if add_dash_anchors and not has_dash_anchors:
anchor_element = etree.Element("a")
anchor_element.set("class", "dashAnchor")
anchor_name = collected_name.split("::")[-1]
suffix = ""
if anchor_name in anchors:
count = anchors.count(anchor_name)
suffix = f" (Overload {count})"
anchors.append(anchor_name)
anchor_name = f"{anchor_name}{suffix}"
anchor_element.set(
"name",
f"//apple_ref/cpp/{collected_type}/{html.escape(anchor_name)}",
)
elements[i].addprevious(anchor_element)
entries.append(
Entry(
cast(str, collected_name),
cast(str, collected_html_path),
cast(str, collected_type),
)
)
with open(html_path, "w") as html_file:
html_file.write(tostring(xml).decode("utf-8")) # pyright: ignore
return entries
class KwargsDocsetProcessor(TypedDict):
"""
Define the keyword arguments for a docset processor.
Parameters
----------
api_directory
Docset *API* path, e.g.``en-US/API``.
docset_name
Docset name, e.g.``UnrealEngineC++.docset``.
online_url
Online redirect *URL*.
output_directory
Docset output directory.
unpacking_directory
Docset unpacking directory.
"""
api_directory: Path
docset_name: str
online_url: str
output_directory: Path
unpacking_directory: Path
def process_cpp_docset(**kwargs: Unpack[KwargsDocsetProcessor]) -> set[Entry]:
"""
Process given *Unreal Engine* *C++* docset.
Other Parameters
----------------
kwargs
See :class:`KwargsProcessor` class documentation.
Returns
-------
:class:`set`
Set of entries.
"""
logger.info("Processing C++ docset...")
api_directory = kwargs["api_directory"]
css_path = api_directory / ".." / ".." / "Include" / "CSS" / "udn_public.css"
with open(css_path, "a") as css_file:
css_file.write(
"""
#maincol {
height: unset !important;
}
#page_head, #navWrapper, #splitter, #footer {
display: none !important;
}
#contentContainer {
margin-left: 0 !important;
}
.toc {
display: none !important;
}
"""
)
html_files = list(api_directory.glob("**/*.html"))
return set(
chain(
*process_map(
process_cpp_html_file,
html_files,
chunksize=16,
)
)
)
def process_blueprint_html_file(
html_path: Path,
collectors: tuple = COLLECTORS_BLUEPRINT,
add_dash_anchors: bool = True,
) -> list[Entry]:
"""
Process given *Unreal Engine* *Blueprint* *HTML* file using given collectors.
Parameters
----------
html_path
*HTML* file path to collect the data from.
collectors
Collectors used to process the *HTML* file.
add_dash_anchors
Whether to add *Dash* anchors to generate a
`TOC <https://kapeli.com/docsets#tableofcontents>`__.
Returns
-------
:class:`list`
List of *Dash* documentation entries.
"""
logger.info('Processing "%s" file...', html_path)
def localiser(link):
"""Update given link to point to an actual *HTML* file"""
index_path = html_path.parent / Path(link) / "index.html"
if not index_path.exists():
return link
return f"{link}/index.html"
xml = read_xml_file(html_path)
xml.rewrite_links(localiser)
api_information = collect_api_information(xml)
has_dash_anchors = len(xml.xpath('.//a[@class="dashAnchor"]')) != 0
entries = []
for collector in collectors:
elements = xml.xpath(collector.predicate)
for i, collection in enumerate(
collector.processor(elements, api_information, html_path)
):
collected_name, collected_html_path = collection
if not Path(collected_html_path).exists():
continue
collected_type = collector.type
if add_dash_anchors and not has_dash_anchors:
anchor_element = etree.Element("a")
anchor_element.set("class", "dashAnchor")
anchor_element.set(
"name",
f"//apple_ref/blueprint/{collected_type}/{html.escape(collected_name)}",
)
elements[i].addprevious(anchor_element)
entries.append(
Entry(
cast(str, collected_name),
cast(str, collected_html_path),
cast(str, collected_type),
)
)
with open(html_path, "w") as html_file:
html_file.write(tostring(xml).decode("utf-8")) # pyright: ignore
return entries
def process_blueprint_docset(**kwargs: Unpack[KwargsDocsetProcessor]) -> set[Entry]:
"""
Process given *Unreal Engine* *Blueprint* docset.
Other Parameters
----------------
kwargs
See :class:`KwargsProcessor` class documentation.
Returns
-------
:class:`set`
Set of entries.
"""
logger.info("Processing Blueprint docset...")
api_directory = kwargs["api_directory"]
css_path = api_directory / ".." / ".." / "Include" / "CSS" / "udn_public.css"
with open(css_path, "a") as css_file:
css_file.write(
"""
#maincol {
height: unset !important;
}
#page_head, #navWrapper, #splitter, #footer {
display: none !important;
}
#contentContainer {
margin-left: 0 !important;
}
.toc {
display: none !important;
}
"""
)
html_files = list(api_directory.glob("**/*.html"))
return set(
chain(
*process_map(
process_blueprint_html_file,
html_files,
max_workers=multiprocessing.cpu_count() - 2,
chunksize=16,
)
)
)
def process_python_docset(**kwargs: Unpack[KwargsDocsetProcessor]) -> set[Entry]:
"""
Process given *Unreal Engine* *Python* docset.
This uses `doc2dash <https://github.com/hynek/doc2dash>`__ *API*.
Other Parameters
----------------
kwargs
See :class:`KwargsProcessor` class documentation.
Returns
-------
:class:`set`
Set of entries.
"""
from doc2dash.__main__ import main
main.callback( # pyright: ignore
source=Path(kwargs["unpacking_directory"]),
force=True,
name=kwargs["docset_name"],
quiet=False,
verbose=True,
destination=kwargs["output_directory"],
add_to_dash=False,
add_to_global=False,
icon=None,
icon_2x=None,
index_page=Path("index.html"),
enable_js=True,
online_redirect_url=kwargs["online_url"],
playground_url=None,
parser_type=None,
full_text_search="off",
)
return set()
def generate_database(
database_path: Path, documents_directory: Path | str, entries: set[Entry]
) -> None:
"""
Generate the *SQLite3* database storing the *Dash* entries.
Parameters
----------
database_path
Path of the *SQLite3* database.
documents_directory
Path of the documents directory, e.g.
``UnrealEngineCpp.docset/Contents/Resources/Documents``.
entries
Entries to add to the *SQLite3* database.
"""
logger.info("Creating Sqlite3 database...")
database = sqlite3.connect(database_path)
cursor = database.cursor()
try:
cursor.execute(
"CREATE TABLE searchIndex("
"id INTEGER PRIMARY KEY, "
"name TEXT, "
"type TEXT, "
"path TEXT);"
)
cursor.execute("CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path);")
except sqlite3.OperationalError as error:
logging.warning(str(error))
documents_directory = f"{documents_directory}/".replace("\\", "/")
for entry in sorted(entries, key=lambda x: x.name):
cursor.execute(
"INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)",
(
entry.name,
entry.type,
entry.path.replace("\\", "/").replace(documents_directory, ""),
),
)
database.commit()
database.close()
def generate_plist(path: Path, mapping: list[tuple[str, str, str]]) -> None:
"""
Generate the *PLIST* file for *Dash*.
Parameters
----------
path
Path of the *PLIST* file, e.g.
``UnrealEngineCpp.docset/Contents/Info.plist``.
mapping
Mapping of the data to store in the *PLIST* file.
"""
logger.info('Generating "%s" plist file...', path)
plist_element = Et.Element("plist")
plist_element.set("version", "1.0")
mapping_element = Et.SubElement(plist_element, "dict")
for key, type_, value in mapping:
key_element = Et.SubElement(mapping_element, "key")
key_element.text = key
if type_ in ["string", "true", "false"]:
value_element = Et.SubElement(mapping_element, type_)
if type_ == "string":
value_element.text = value
xml = minidom.parseString( # noqa: S318
Et.tostring(plist_element, "utf-8")
).toprettyxml(indent="\t")
xml = xml.split("\n", 1)[1]
header = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" '
'"http://www.apple.com/DTDs/Propertylist-1.0.dtd">\n'
)
with open(path, "w") as plist_file:
plist_file.write(header)
plist_file.write(xml)
@click.command()
@click.option(
"--input",
required=True,
type=click.Path(exists=True),
help='Input "tgz" file to generate the docset from.',
)
@click.option(
"--output",
required=True,
type=click.Path(exists=True),
help="Output directory to generate to.",
)
def generate_docset(input: str, output: str) -> None: # noqa: A002
"""
Generate the *Dash* docset from given input *Unreal Engine* *tgz* file.
Parameters
----------
input
*Unreal Engine* *tgz* file.
output
*Dash* docset output directory.
"""
if "cpp" in Path(input).stem.lower():
docset_type = "c++"
elif "blueprint" in Path(input).stem.lower():
docset_type = "blueprint"
elif "python" in Path(input).stem.lower():
docset_type = "python"
else:
logging.error("Unsupported docset type, exiting!")
return
output_directory = Path(output)
docset_name = f"UnrealEngine{docset_type.title()}API.docset"
docset_directory = output_directory / docset_name
contents_directory = docset_directory / "Contents"
resources_directory = contents_directory / "Resources"
documents_directory = resources_directory / "Documents"
documents_directory.mkdir(parents=True, exist_ok=True)
if docset_type == "c++":
api_directory = documents_directory / "en-US" / "API"
label = "C++"
processor = process_cpp_docset
online_url = "https://docs.unrealengine.com/en-US/API"
unpacking_directory = documents_directory
elif docset_type == "blueprint":
api_directory = documents_directory / "en-US" / "BlueprintAPI"