src/doc/commands: clean `commands_docdown`
[nit.git] / src / doc / commands / commands_docdown.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Doc down related queries
16 module commands_docdown
17
18 import commands_md
19 import markdown
20
21 # Retrieve the MDoc summary
22 #
23 # List all MarkdownHeading found and their ids.
24 class CmdSummary
25 super CmdComment
26
27 # Markdown processor used to parse the headlines
28 var markdown_processor: MarkdownProcessor is writable
29
30 # Resulting summary
31 #
32 # Associates each headline to its id.
33 var summary: nullable ArrayMap[String, HeadLine] = null is optional, writable
34
35 redef fun init_command do
36 var res = super
37 if not res isa CmdSuccess then return res
38 var mentity = self.mentity.as(not null)
39
40 var mdoc = self.mdoc
41 if mdoc == null then
42 mdoc = if fallback then mentity.mdoc_or_fallback else mentity.mdoc
43 self.mdoc = mdoc
44 end
45 if mdoc == null then return new WarningNoMDoc(mentity)
46
47 markdown_processor.process(mdoc.md_documentation.write_to_string)
48
49 var summary = new ArrayMap[String, HeadLine]
50 summary.add_all markdown_processor.decorator.headlines
51 self.summary = summary
52 return res
53 end
54 end