1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Doc down related queries
16 module commands_docdown
18 import commands
::commands_parser
19 import commands
::commands_html
20 import commands
::commands_md
22 intrude import markdown
::wikilinks
24 # Retrieve the MDoc summary
26 # List all MarkdownHeading found and their ids.
30 # Markdown processor used to parse the headlines
31 var markdown_processor
: nullable MarkdownProcessor = null is optional
, writable
35 # Associates each headline to its id.
36 var summary
: nullable ArrayMap[String, HeadLine] = null is optional
, writable
38 redef fun init_command
do
40 if not res
isa CmdSuccess then return res
41 var mentity
= self.mentity
.as(not null)
43 var markdown_processor
= self.markdown_processor
44 if markdown_processor
== null then
45 markdown_processor
= new MarkdownProcessor
46 self.markdown_processor
= markdown_processor
51 mdoc
= if fallback
then mentity
.mdoc_or_fallback
else mentity
.mdoc
54 if mdoc
== null then return new WarningNoMDoc(mentity
)
56 markdown_processor
.process
(mdoc
.md_documentation
.write_to_string
)
58 var summary
= new ArrayMap[String, HeadLine]
59 summary
.add_all markdown_processor
.decorator
.headlines
60 self.summary
= summary
65 # Custom Markdown processor able to process doc commands
69 redef type PROCESSOR: CmdMarkdownProcessor
71 # Model used by wikilink commands to find entities
74 # Filter to apply if any
75 var filter
: nullable ModelFilter
77 redef fun add_span_code
(v
, buffer
, from
, to
) do
78 var text
= new FlatBuffer
79 buffer
.read
(text
, from
, to
)
80 var name
= text
.write_to_string
81 name
= name
.replace
("nullable ", "")
82 var mentity
= try_find_mentity
(name
)
83 if mentity
== null then
87 v
.emit_text mentity
.html_link
.write_to_string
92 private fun try_find_mentity
(text
: String): nullable MEntity do
93 var mentity
= model
.mentity_by_full_name
(text
, filter
)
94 if mentity
!= null then return mentity
96 var mentities
= model
.mentities_by_name
(text
, filter
)
97 if mentities
.is_empty
then
99 else if mentities
.length
> 1 then
100 # TODO smart resolve conflicts
102 return mentities
.first
105 redef fun add_wikilink
(v
, token
) do
106 v
.render_wikilink
(token
, model
)
110 # Same as `InlineDecorator` but with wikilink commands handling
111 class CmdInlineDecorator
112 super InlineDecorator
114 redef type PROCESSOR: CmdMarkdownProcessor
116 # Model used by wikilink commands to find entities
119 redef fun add_wikilink
(v
, token
) do
120 v
.render_wikilink
(token
, model
)
124 # Custom MarkdownEmitter for commands
125 class CmdMarkdownProcessor
126 super MarkdownProcessor
128 # Parser used to process doc commands
129 var parser
: CommandParser
132 fun render_wikilink
(token
: TokenWikiLink, model
: Model) do
133 var link
= token
.link
134 if link
== null then return
135 var name
= token
.name
136 if name
!= null then link
= "{name} | {link}"
138 var command
= parser
.parse
(link
.write_to_string
)
139 var error
= parser
.error
141 if error
isa CmdError then
142 emit_text error
.to_html
.write_to_string
145 if error
isa CmdWarning then
146 emit_text error
.to_html
.write_to_string
148 add command
.as(not null).to_html
153 # Read `self` between `nstart` and `nend` (excluded) and writte chars to `out`.
154 private fun read
(out
: FlatBuffer, nstart
, nend
: Int): Int do
156 while pos
< length
and pos
< nend
do
160 if pos
== length
then return -1