Code source wiki de MacroService

Version 1.1 par Xavier Richard le 01/11/2017 à 21:13

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 ($macroManager = $services.component.getInstance('org.xwiki.rendering.macro.MacroManager'))
24 #set ($syntax = $services.rendering.resolveSyntax($syntaxId))
25 #set ($data = {})
26 #set ($allMacrosExcludedCategories = [])
27 #set ($discard = $allMacrosExcludedCategories.add("#maybeTranslate('rendering.macroCategory.Internal' 'Internal')"))
28 #set ($discard = $allMacrosExcludedCategories.add("#maybeTranslate('rendering.macroCategory.Deprecated' 'Deprecated')"))
29 #set ($discard = $data.put('options', { 'allMacrosExcludedCategories' : $allMacrosExcludedCategories }))
30 #set ($macroList = [])
31 #foreach ($macroId in $macroManager.getMacroIds($syntax))
32 #set ($macroDescriptor = $macroManager.getMacro($macroId).descriptor)
33 #set ($macroTranslationKey = "rendering.macro.$macroId")
34 #set ($macroCategoryTranslationKey = "rendering.macroCategory.$macroDescriptor.defaultCategory")
35 #set ($discard = $macroList.add({
36 'id': $macroId,
37 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)",
38 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)",
39 'defaultCategory': "#maybeTranslate($macroCategoryTranslationKey $macroDescriptor.defaultCategory)"
40 }))
41 #end
42 #set ($macroList = $sorttool.sort($macroList, 'name'))
43 #set ($discard = $data.put('list', $macroList))
44 #end
45
46 #macro (maybeGetMacroDescriptor $macroIdAsString)
47 #set ($macroIdFactory = $services.component.getInstance('org.xwiki.rendering.macro.MacroIdFactory'))
48 #set ($macroId = $macroIdFactory.createMacroId($macroIdAsString))
49 #set ($macroManager = $services.component.getInstance('org.xwiki.rendering.macro.MacroManager'))
50 #if ($macroManager.exists($macroId))
51 #getMacroDescriptor($macroId)
52 #elseif ($macroId.syntax)
53 ## Try the macro id without the syntax.
54 #set ($macroId = $macroIdFactory.createMacroId($macroId.id))
55 #if ($macroManager.exists($macroId))
56 #getMacroDescriptor($macroId)
57 #end
58 #end
59 #end
60
61 #macro (getMacroDescriptor $macroId)
62 #set ($macro = $macroManager.getMacro($macroId))
63 #set ($macroDescriptor = $macro.descriptor)
64 ## Translate the macro name and description.
65 #set ($macroTranslationKey = "rendering.macro.$macroId")
66 #set ($data = {
67 'id': $macroDescriptor.id,
68 'name': "#maybeTranslate(""${macroTranslationKey}.name"" $macroDescriptor.name)",
69 'description': "#maybeTranslate(""${macroTranslationKey}.description"" $macroDescriptor.description)",
70 'defaultCategory': $macroDescriptor.defaultCategory,
71 'supportsInlineMode': $macro.supportsInlineMode(),
72 'parameterDescriptorMap': {}
73 })
74 #if ($macroDescriptor.contentDescriptor)
75 ## Translate the content label and description.
76 ## Treat the macro content as if it is the last macro parameter.
77 #set ($data.contentDescriptor = {
78 'name': "#maybeTranslate('rendering.macroContent' 'Content')",
79 'description': "#maybeTranslate(""${macroTranslationKey}.content.description""
80 $macroDescriptor.contentDescriptor.description)",
81 'mandatory': $macroDescriptor.contentDescriptor.mandatory,
82 'index': $macroDescriptor.parameterDescriptorMap.size()
83 })
84 #end
85 #foreach ($entry in $macroDescriptor.parameterDescriptorMap.entrySet())
86 #set ($parameterDescriptor = $entry.value)
87 ## Translate the parameter name and description.
88 #set ($parameterTranslationKey = "${macroTranslationKey}.parameter.$parameterDescriptor.id")
89 #set ($translatedParameterDescriptor = {
90 'id': $parameterDescriptor.id,
91 'name': "#maybeTranslate(""${parameterTranslationKey}.name"" $parameterDescriptor.name)",
92 'description': "#maybeTranslate(""${parameterTranslationKey}.description"" $parameterDescriptor.description)",
93 'mandatory': $parameterDescriptor.mandatory,
94 'defaultValue': $parameterDescriptor.defaultValue,
95 'type': $parameterDescriptor.parameterType,
96 'index': $foreach.index
97 })
98 #if ($parameterDescriptor.parameterType.isEnum())
99 ## Collect the allowed parameter values.
100 #set ($translatedParameterDescriptor.type = 'enum')
101 #if ($parameterDescriptor.defaultValue)
102 #set ($parameterValues = $parameterDescriptor.defaultValue.values())
103 #else
104 ## A parameter of type enum that doesn't have a default value is very unlikely. We attempt to read the list of
105 ## possible values from the enum type in this case, which is currently forbidden, but at least it will generate
106 ## a warning in the logs that will help us investigate the problem.
107 #set ($parameterValues = $parameterDescriptor.parameterType.getEnumConstants())
108 #end
109 #set ($translatedParameterDescriptor.values = [])
110 ## Translate the allowed parameter values.
111 #foreach ($parameterValue in $parameterValues)
112 #set ($discard = $translatedParameterDescriptor.values.add({
113 'id': $parameterValue,
114 'label': "#maybeTranslate(""${parameterTranslationKey}.value.$parameterValue"" $parameterValue)"
115 }))
116 #end
117 #elseif (($parameterDescriptor.defaultValue == 'false' || $parameterDescriptor.defaultValue == 'true')
118 && $macroDescriptor.parametersBeanClass.getSimpleName() == 'WikiMacroParameters')
119 #set ($translatedParameterDescriptor.type = 'boolean')
120 #set ($translatedParameterDescriptor.defaultValue = $parameterDescriptor.defaultValue == 'true')
121 #end
122 ## Make sure the key is lowercase (for XWiki <9.0).
123 ## See XWIKI-13990: Inconsistency between Java-based and Wiki-based rendering macros regarding the parameter
124 ## descriptor map keys
125 #set ($discard = $data.parameterDescriptorMap.put($entry.key.toLowerCase(), $translatedParameterDescriptor))
126 #end
127 #end
128
129 #macro (maybeTranslate $key $defaultValue)
130 #if ($services.localization.get($key))
131 $services.localization.render($key)##
132 #else
133 $!defaultValue##
134 #end
135 #end
136 {{/velocity}}
137
138 {{velocity wiki="false"}}
139 #if ("$!request.data" != '')
140 #set ($data = $NULL)
141 #if ($request.data == 'list')
142 #getMacroList($request.syntaxId)
143 #elseif ($request.data == 'descriptor')
144 #maybeGetMacroDescriptor($request.macroId)
145 #end
146 #if ($data)
147 #set ($discard = $response.setContentType('application/json'))
148 $jsontool.serialize($data)
149 #else
150 $response.sendError(404)
151 #end
152 #end
153 {{/velocity}}