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 # Documentation associated to an entity.
16 module model
::descriptions
21 # Documentation associated to an entity.
23 # The documentation is written in Markdown format.
26 # var doc = new Documentation
28 # doc.brief_description = "Do something."
29 # doc.detailed_description = ["Do not lunch a rocket."]
30 # assert doc.brief_description == "Do something."
31 # assert doc.detailed_description == ["Do not lunch a rocket."]
32 # assert doc.to_json == """["Do something.","Do not lunch a rocket."]"""
34 # doc.brief_description = ""
35 # doc.detailed_description = ["The answer is `42`."]
36 # assert doc.brief_description == "The answer is `42`."
37 # assert doc.detailed_description == ["The answer is `42`."]
38 # assert doc.to_json == """["The answer is `42`."]"""
40 # doc.detailed_description = ["The answer is `42`."]
41 # doc.brief_description = ""
42 # assert doc.brief_description == "The answer is `42`."
43 # assert doc.detailed_description == ["The answer is `42`."]
44 # assert doc.to_json == """["The answer is `42`."]"""
46 # doc.detailed_description = new Array[String]
47 # doc.brief_description = ""
49 # assert doc.brief_description == ""
50 # assert doc.detailed_description == new Array[String]
51 # assert doc.to_json == "[]"
56 private var content
= new JsonStringArray
57 private var has_brief_description
: Bool = false
59 # The brief description.
61 # If it is empty, the first element of `detailed_description` will be used
62 # as brief description.
63 fun brief_description
=(brief_description
: String) do
64 if brief_description
.is_empty
then
65 if has_brief_description
then
67 has_brief_description
= false
69 else if has_brief_description
then
70 content
.first
= brief_description
72 content
.unshift
(brief_description
)
73 has_brief_description
= true
77 # The brief description.
78 fun brief_description
: String do
79 if not is_empty
then return content
.first
83 # The detailed description.
85 # Each element should represent a block.
86 fun detailed_description
=(detailed_description
: SequenceRead[String]) do
87 if has_brief_description
then
88 while content
.length
> 1 do content
.pop
92 content
.add_all
(detailed_description
)
95 # The detailed description.
97 # Each element should represent a block.
98 fun detailed_description
: SequenceRead[String] do
99 if not has_brief_description
then return content
100 if content
.length
> 1 then return content
.subarray
(1, content
.length
- 1)
101 return new Array[String]
104 # Add a block of detailed description.
105 fun add
(block
: String) do content
.add block
107 # Is the documentation empty?
108 fun is_empty
: Bool do return content
.is_empty
110 redef fun serialize_to
(v
) do content
.serialize_to v
111 redef fun accept_json_serializer
(v
) do content
.serialize_to v
114 # A `Serializable` array of strings.
115 private class JsonStringArray
116 super JsonSequenceRead[String]