BrightSide Workbench Full Report + Source Code
WikiProcessors.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.parser.wiki;
20 
21 import java.util.Map;
22 import org.turro.elephant.context.IConstructor;
23 import org.turro.parser.MacroParser;
24 import org.turro.plugin.contacts.IContact;
25 import org.turro.reflection.Instances;
26 import org.turro.util.Final;
27 
32 public class WikiProcessors {
33 
34  public WikiProcessors constructor(IConstructor constructor) {
35  this.constructor = constructor;
36  return this;
37  }
38 
39  public WikiProcessors contact(IContact contact) {
40  this.contact = contact;
41  return this;
42  }
43 
44  public String resolve(String value) {
45  Final<String> result = Final.empty();
46  if("@w{help}".equals(value)) {
47  result.set(help());
48  } else if(value.contains(MACRO_START)) {
49  MacroParser.of(MACRO_START, MACRO_END).process(value, content -> {
50  String name = MacroParser.macroName(content);
51  Map<String, String> pars = MacroParser.parameterMap(content);
52  for(IMacroProcessor processor : Instances.cached().byInterface(IMacroProcessor.class, IMacroProcessor.class)) {
53  String expanded = processor.expand(constructor, contact, name, pars);
54  if(expanded != null) {
55  result.set(expanded);
56  break;
57  }
58  }
59  });
60  }
61  return result.isEmpty() ? notFound(value) : result.get();
62  }
63 
64  public String help() {
65  StringBuilder sb = new StringBuilder();
66  for(IMacroProcessor processor : Instances.cached().byInterface(IMacroProcessor.class, IMacroProcessor.class)) {
67  for(String name : processor.locateNames(constructor)) {
68  String expanded = processor.help(constructor, name);
69  if(expanded != null) {
70  sb.append(expanded);
71  }
72  }
73  }
74  return sb.toString();
75  }
76 
77 
78  /* Utils */
79 
80  private String notFound(String value) {
81  return "<span style='color:red'>(WM " + value + ")</span>";
82  }
83 
84  /* Factory */
85 
86  public static WikiProcessors instance() {
87  return new WikiProcessors();
88  }
89 
90  private static final String MACRO_START = "@w{", MACRO_END = "}";
91 
92  private IConstructor constructor;
93  private IContact contact;
94 
95  private WikiProcessors() {}
96 
97 }
WikiProcessors contact(IContact contact)
WikiProcessors constructor(IConstructor constructor)