3
3
namespace SymfonyDocs \Command ;
4
4
5
5
use Doctrine \RST \Builder ;
6
- use Doctrine \RST \Configuration ;
6
+ use Doctrine \RST \Event \PostBuildRenderEvent ;
7
+ use Doctrine \RST \Event \PostNodeRenderEvent ;
8
+ use Doctrine \RST \Event \PostParseDocumentEvent ;
9
+ use Doctrine \RST \Event \PreBuildParseEvent ;
10
+ use Doctrine \RST \Event \PreBuildRenderEvent ;
11
+ use Doctrine \RST \Event \PreBuildScanEvent ;
7
12
use Symfony \Component \Console \Command \Command ;
8
13
use Symfony \Component \Console \Helper \ProgressBar ;
9
14
use Symfony \Component \Console \Input \InputInterface ;
12
17
use Symfony \Component \Console \Style \SymfonyStyle ;
13
18
use Symfony \Component \Filesystem \Filesystem ;
14
19
use Symfony \Component \Finder \Finder ;
15
- use SymfonyDocs \KernelFactory ;
16
- use SymfonyDocs \SymfonyDocConfiguration ;
17
20
use SymfonyDocs \JsonGenerator ;
21
+ use SymfonyDocs \KernelFactory ;
18
22
19
23
/**
20
24
* Class ParseDoc
@@ -29,16 +33,21 @@ class ParseDoc extends Command
29
33
private $ finder ;
30
34
/** @var ProgressBar */
31
35
private $ progressBar ;
36
+ /** @var Builder */
37
+ private $ builder ;
38
+ /** @var OutputInterface */
39
+ private $ output ;
32
40
private $ sourceDir ;
33
41
private $ htmlOutputDir ;
34
42
private $ jsonOutputDir ;
43
+ private $ parsedFiles = [];
35
44
36
45
public function __construct ()
37
46
{
38
47
parent ::__construct (self ::$ defaultName );
39
48
40
49
$ this ->filesystem = new Filesystem ();
41
- $ this ->finder = new Finder ();
50
+ $ this ->finder = new Finder ();
42
51
}
43
52
44
53
protected function configure ()
@@ -48,13 +57,13 @@ protected function configure()
48
57
$ this
49
58
->addOption ('source-dir ' , null , InputOption::VALUE_REQUIRED , 'RST files Source directory ' , __DIR__ .'/../../.. ' )
50
59
->addOption ('html-output-dir ' , null , InputOption::VALUE_REQUIRED , 'HTML files output directory ' , __DIR__ .'/../../html ' )
51
- ->addOption ('json-output-dir ' , null , InputOption::VALUE_REQUIRED , 'JSON files output directory ' , __DIR__ .'/../../json ' )
52
- ;
60
+ ->addOption ('json-output-dir ' , null , InputOption::VALUE_REQUIRED , 'JSON files output directory ' , __DIR__ .'/../../json ' );
53
61
}
54
62
55
63
protected function initialize (InputInterface $ input , OutputInterface $ output )
56
64
{
57
65
$ this ->io = new SymfonyStyle ($ input , $ output );
66
+ $ this ->output = $ output ;
58
67
59
68
$ this ->sourceDir = $ this ->getRealAbsolutePath ($ input ->getOption ('source-dir ' ));
60
69
if (!$ this ->filesystem ->exists ($ this ->sourceDir )) {
@@ -70,30 +79,36 @@ protected function initialize(InputInterface $input, OutputInterface $output)
70
79
if ($ this ->filesystem ->exists ($ this ->jsonOutputDir )) {
71
80
$ this ->filesystem ->remove ($ this ->jsonOutputDir );
72
81
}
82
+
83
+ $ this ->builder = new Builder (KernelFactory::createKernel ());
84
+ $ eventManager = $ this ->builder ->getConfiguration ()->getEventManager ();
85
+ $ eventManager ->addEventListener (
86
+ [PostParseDocumentEvent::POST_PARSE_DOCUMENT ],
87
+ $ this
88
+ );
89
+ $ eventManager ->addEventListener (
90
+ [PreBuildRenderEvent::PRE_BUILD_RENDER ],
91
+ $ this
92
+ );
73
93
}
74
94
75
95
protected function execute (InputInterface $ input , OutputInterface $ output )
76
96
{
77
- $ builder = KernelFactory::createKernel ();
78
-
79
- // $builder->addHook([$this, 'handleProgressBar']);
80
-
81
97
$ this ->finder ->in ($ input ->getOption ('source-dir ' ))
82
98
->exclude (['_build ' , '.github ' , '.platform ' , '_images ' ])
83
99
->notName ('*.rst.inc ' )
84
100
->name ('*.rst ' );
85
101
86
- $ this ->io ->note (sprintf ('Start parsing into html %d rst files ' , $ this ->finder ->count ()));
102
+ $ this ->io ->note (sprintf ('Start parsing %d rst files ' , $ this ->finder ->count ()));
87
103
$ this ->progressBar = new ProgressBar ($ output , $ this ->finder ->count ());
88
104
89
- $ builder ->build (
105
+ $ this -> builder ->build (
90
106
$ this ->sourceDir ,
91
107
$ this ->htmlOutputDir
92
108
);
93
109
94
- $ this ->progressBar ->finish ();
95
110
$ this ->io ->newLine (2 );
96
- $ this ->io ->success ('Parse into html complete ' );
111
+ $ this ->io ->success ('HTML rendering complete! ' );
97
112
98
113
foreach ($ this ->finder as $ file ) {
99
114
$ htmlFile = str_replace ([$ this ->sourceDir , '.rst ' ], [$ this ->htmlOutputDir , '.html ' ], $ file ->getRealPath ());
@@ -104,7 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
104
119
105
120
$ this ->io ->note ('Start transforming doc into json files ' );
106
121
$ this ->progressBar = new ProgressBar ($ output , $ this ->finder ->count ());
107
- $ jsonGenerator = new JsonGenerator ($ builder ->getDocuments ()->getAll ());
122
+ $ jsonGenerator = new JsonGenerator ($ this -> builder ->getDocuments ()->getAll ());
108
123
$ jsonGenerator ->generateJson ($ this ->htmlOutputDir , $ this ->jsonOutputDir , $ this ->progressBar );
109
124
$ this ->io ->newLine (2 );
110
125
$ this ->io ->success ('Parse process complete ' );
@@ -125,4 +140,27 @@ private function getRealAbsolutePath(string $path): string
125
140
)
126
141
);
127
142
}
143
+
144
+ public function postParseDocument (PostParseDocumentEvent $ postParseDocumentEvent )
145
+ {
146
+ $ file = $ postParseDocumentEvent ->getDocumentNode ()->getEnvironment ()->getCurrentFileName ();
147
+ if (!\in_array ($ file , $ this ->parsedFiles )) {
148
+ $ this ->parsedFiles [] = $ postParseDocumentEvent ->getDocumentNode ()->getEnvironment ()->getCurrentFileName ();
149
+ $ this ->progressBar ->advance ();
150
+ }
151
+ }
152
+
153
+ public function preBuildRender ()
154
+ {
155
+ $ eventManager = $ this ->builder ->getConfiguration ()->getEventManager ();
156
+ $ eventManager ->removeEventListener (
157
+ [PostParseDocumentEvent::POST_PARSE_DOCUMENT ],
158
+ $ this
159
+ );
160
+
161
+ $ this ->progressBar ->finish ();
162
+
163
+ $ this ->io ->newLine (2 );
164
+ $ this ->io ->note ('Start rendering in HTML... ' );
165
+ }
128
166
}
0 commit comments