Code source wiki de MacroService

Version 2.1 par Xavier Richard le 01/12/2018 à 11:31

Afficher les derniers auteurs
1 {{velocity output="false"}}
2 ## ================================================================
3 ## Returned JSON format:
4 ##
5 ## {
6 ## 'options': {
7 ## 'allMacrosExcludedCategories': [
8 ## (translated category name), ...
9 ## ]
10 ## },
11 ## 'list': [
12 ## {
13 ## 'id': (macro id),
14 ## 'name': (translated macro name),
15 ## 'description': (translated macro description),
16 ## 'defaultCategory': (translated macro category),
17 ## },
18 ## ...
19 ## ]
20 ## }
21 ## ================================================================
22 #macro (getMacroList $syntaxId)
23 #set ($syntax = $services.rendering.resolveSyntax($syntaxId))
24 #set ($macroDescriptors = $services.rendering.getMacroDescriptors($syntax))
25 #if (!$macroDescriptors)
26 ## Before XWiki 9.7RC1 we had to use APIs that require programming rights.
27 #set ($macroDescriptors = [])
28 #try()
29 #set ($macroManager = $services.component.getInstance('org.xwiki.rendering.macro.MacroManager'))
30 #foreach ($macroId in $macroManager.getMacroIds($syntax))
31 #set ($macroDescriptor = $macroManager.getMacro($macroId).descriptor)
32 #set ($discard = $macroDescriptors.add($macroDescriptor))
33 #end
34 #end
35 #end
36 #set ($data = {})
37 #set ($allMacrosExcludedCategories = [])
38 #set ($discard = $allMacrosExcludedCategories.add("#maybeTranslate('rendering.macroCategory.Internal' 'Internal')"))
39 #set ($discard = $allMacrosExcludedCategories.add("#maybeTranslate('rendering.macroCategory.Deprecated' 'Deprecated')"))
40 #set ($discard = $data.put('options', { 'allMacrosExcludedCategories' : $allMacrosExcludedCategories }))
41 #set ($macroList = [])
42 #foreach ($macroDescriptor in $macroDescriptors)
43 #set ($macroTranslationKey = "rendering.macro.$macroDescriptor.id")
44 #set ($macroCategoryTranslationKey = "rendering.macroCategory.$macroDescriptor.defaultCategory")
45 #set ($discard = $macroList.add({
46 'id': $macroDescriptor.id,
47 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)",
48 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)",
49 'defaultCategory': "#maybeTranslate($macroCategoryTranslationKey $macroDescriptor.defaultCategory)"
50 }))
51 #end
52 #set ($macroList = $sorttool.sort($macroList, 'name'))
53 #set ($discard = $data.put('list', $macroList))
54 #end
55
56 #macro (maybeGetMacroDescriptor $macroIdAsString)
57 #set ($xmacro = $NULL)
58 #set ($macroDescriptor = $NULL)
59 #set ($macroId = $services.rendering.resolveMacroId($macroIdAsString))
60 #if ($macroId)
61 #set ($macroDescriptor = $services.rendering.getMacroDescriptor($macroId))
62 #if (!$macroDescriptor && $macroId.syntax)
63 ## Try the macro id without the syntax.
64 #set ($macroId = $services.rendering.resolveMacroId($macroId.id))
65 #set ($macroDescriptor = $services.rendering.getMacroDescriptor($macroId))
66 #end
67 #else
68 ## Either the macro id could not be resolved (unlikely) or we are on an older XWiki instance (before 10.10RC1) where
69 ## we had to use APIs that require programming rights.
70 #getMacroWithPR($macroIdAsString)
71 #if ($xmacro)
72 #set ($macroDescriptor = $xmacro.descriptor)
73 #end
74 #end
75 #if ($macroDescriptor)
76 #getMacroDescriptor($macroDescriptor)
77 #if ($xmacro)
78 ## supportsInlineMode was not exposed on the macro descriptor before XWiki 10.10RC1.
79 #set ($data.supportsInlineMode = $xmacro.supportsInlineMode())
80 #end
81 #end
82 #end
83
84 #macro (getMacroWithPR $macroIdAsString)
85 #set ($xmacro = $NULL)
86 #try()
87 #set ($macroIdFactory = $services.component.getInstance('org.xwiki.rendering.macro.MacroIdFactory'))
88 #set ($macroId = $macroIdFactory.createMacroId($macroIdAsString))
89 #set ($macroManager = $services.component.getInstance('org.xwiki.rendering.macro.MacroManager'))
90 #if ($macroManager.exists($macroId))
91 #set ($xmacro = $macroManager.getMacro($macroId))
92 #elseif ($macroId.syntax)
93 ## Try the macro id without the syntax.
94 #set ($macroId = $macroIdFactory.createMacroId($macroId.id))
95 #if ($macroManager.exists($macroId))
96 #set ($xmacro = $macroManager.getMacro($macroId))
97 #end
98 #end
99 #end
100 #end
101
102 #macro (getMacroDescriptor $macroDescriptor)
103 ## Translate the macro name and description.
104 #set ($macroTranslationKey = "rendering.macro.$macroDescriptor.id")
105 #set ($data = {
106 'id': $macroDescriptor.id,
107 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)",
108 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)",
109 'defaultCategory': $macroDescriptor.defaultCategory,
110 'supportsInlineMode': $macroDescriptor.supportsInlineMode(),
111 'parameterDescriptorMap': {}
112 })
113 #if ($macroDescriptor.contentDescriptor)
114 ## Translate the content label and description.
115 ## Treat the macro content as if it is the last macro parameter.
116 #set ($data.contentDescriptor = {
117 'name': "#maybeTranslate('rendering.macroContent' 'Content')",
118 'description': "#maybeTranslate(""${macroTranslationKey}.content.description""
119 $macroDescriptor.contentDescriptor.description)",
120 'mandatory': $macroDescriptor.contentDescriptor.mandatory,
121 'type': "$!macroDescriptor.contentDescriptor.type",
122 'index': $macroDescriptor.parameterDescriptorMap.size()
123 })
124 #end
125 #foreach ($entry in $macroDescriptor.parameterDescriptorMap.entrySet())
126 #set ($parameterDescriptor = $entry.value)
127 ## Translate the parameter name and description.
128 #set ($parameterTranslationKey = "${macroTranslationKey}.parameter.$parameterDescriptor.id")
129 #set ($translatedParameterDescriptor = {
130 'id': $parameterDescriptor.id,
131 'name': "#maybeTranslate(""${parameterTranslationKey}.name"" $parameterDescriptor.name)",
132 'description': "#maybeTranslate(""${parameterTranslationKey}.description"" $parameterDescriptor.description)",
133 'mandatory': $parameterDescriptor.mandatory,
134 'defaultValue': $parameterDescriptor.defaultValue,
135 'type': $parameterDescriptor.parameterType,
136 'index': $foreach.index
137 })
138 #if ($parameterDescriptor.parameterType.isEnum())
139 ## Collect the allowed parameter values.
140 #set ($translatedParameterDescriptor.type = 'enum')
141 #if ($parameterDescriptor.defaultValue)
142 #set ($parameterValues = $parameterDescriptor.defaultValue.values())
143 #else
144 ## A parameter of type enum that doesn't have a default value is very unlikely. We attempt to read the list of
145 ## possible values from the enum type in this case, which is currently forbidden, but at least it will generate
146 ## a warning in the logs that will help us investigate the problem.
147 #set ($parameterValues = $parameterDescriptor.parameterType.getEnumConstants())
148 #end
149 #set ($translatedParameterDescriptor.values = [])
150 ## Translate the allowed parameter values.
151 #foreach ($parameterValue in $parameterValues)
152 #set ($discard = $translatedParameterDescriptor.values.add({
153 'id': $parameterValue,
154 'label': "#maybeTranslate(""${parameterTranslationKey}.value.$parameterValue"" $parameterValue)"
155 }))
156 #end
157 #elseif (($parameterDescriptor.defaultValue == 'false' || $parameterDescriptor.defaultValue == 'true')
158 && $macroDescriptor.parametersBeanClass.getSimpleName() == 'WikiMacroParameters')
159 #set ($translatedParameterDescriptor.type = 'boolean')
160 #set ($translatedParameterDescriptor.defaultValue = $parameterDescriptor.defaultValue == 'true')
161 #end
162 ## Make sure the key is lowercase (for XWiki <9.0).
163 ## See XWIKI-13990: Inconsistency between Java-based and Wiki-based rendering macros regarding the parameter
164 ## descriptor map keys
165 #set ($discard = $data.parameterDescriptorMap.put($entry.key.toLowerCase(), $translatedParameterDescriptor))
166 #end
167 #end
168
169 #macro (maybeTranslate $key $defaultValue)
170 #if ($services.localization.get($key))
171 $services.localization.render($key)##
172 #else
173 $!defaultValue##
174 #end
175 #end
176 {{/velocity}}
177
178 {{velocity wiki="false"}}
179 #if ("$!request.data" != '')
180 #set ($data = $NULL)
181 #if ($request.data == 'list')
182 #getMacroList($request.syntaxId)
183 #elseif ($request.data == 'descriptor')
184 #maybeGetMacroDescriptor($request.macroId)
185 #end
186 #if ($data)
187 #set ($discard = $response.setContentType('application/json'))
188 $jsontool.serialize($data)
189 #else
190 $response.sendError(404)
191 #end
192 #end
193 {{/velocity}}