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.
16 module model
::linked_text
21 abstract class LinkedText
24 # All link in the text.
26 # Do not edit directly.
27 var links
: Sequence[nullable Link] = new Array[nullable Link]
29 # Remove all the parts.
35 # Remove the first part.
37 var text
= self["text"]
38 assert text
isa JsonArray
46 # Remove the last part.
48 var text
= self["text"]
49 assert text
isa JsonArray
57 # Remove the part at the specified index.
58 fun remove_part_at
(index
: Int) do
59 var text
= self["text"]
60 assert text
isa JsonArray
62 links
.remove_at
(index
)
68 # Change a part of text.
72 # * `index` : the index of the part.
73 # * `content` : textual content.
74 # * `refid` : `model_id` of the linked entity or `""`.
75 fun set_part
(index
: Int, content
: String, refid
: String) do
76 var text
= self["text"]
77 assert text
isa JsonArray
79 if not refid
.is_empty
then
80 links
[index
] = create_link
(links
.length
, refid
)
86 # Append a part of text.
90 # * `content` : textual content.
91 # * `refid` : `model_id` of the linked entity or `""`.
92 fun add_part
(content
: String, refid
: String) do
93 var text
= self["text"]
99 assert text
isa JsonArray
101 if not refid
.is_empty
then
102 links
.add
(create_link
(links
.length
, refid
))
108 # Create a link to the specified entity.
109 protected fun create_link
(rank
:Int, refid
: String): Link is abstract
112 var text
= self["text"]
114 if text
isa JsonArray then
121 redef fun put_in_graph
do
124 if link
isa Link then
130 redef fun put_edges
do
132 for i
in [0..links
.length
[ do
134 if link
isa Link then
136 graph
.add_edge
(self, "LINK", link
)
146 # * `refid` : `model_id` of the linked entity.
154 redef fun put_edges
do
155 graph
.add_edge
(self, "TARGET", graph
.by_id
[refid
])
158 # Specify the rank (index) of the parameter in the signature.
160 # Called by `LinkedText.put_edges`.
161 private fun rank
=(rank
: Int) do