Skip to content

Commit

Permalink
feat: implementation of %get_current_theme() function
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroques committed Nov 22, 2024
1 parent 20d5917 commit 50ded0a
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 57 deletions.
98 changes: 98 additions & 0 deletions src/net/sourceforge/plantuml/preproc/ReadLineWithYamlHeader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2024, Arnaud Roques
*
* Project Info: https://plantuml.com
*
* If you like this project or if you find it useful, you can support us at:
*
* https://plantuml.com/patreon (only 1$ per month!)
* https://plantuml.com/paypal
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.preproc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.plantuml.text.StringLocated;

public class ReadLineWithYamlHeader implements ReadLine {

private final ReadLine source;
// Right now, the header is not used
// It will be used in next releases
private List<String> yamlHeader;
private final Map<String, String> metadata = new LinkedHashMap<>();

public ReadLineWithYamlHeader(ReadLine source) {
this.source = source;
}

@Override
public void close() throws IOException {
source.close();
}

@Override
public StringLocated readLine() throws IOException {
StringLocated line = source.readLine();
if (yamlHeader == null) {
// At this point, "line" is the very first line of the file
yamlHeader = new ArrayList<String>();
if (isSeparator(line)) {
do {
line = source.readLine();
if (line == null || isSeparator(line))
break;
final String tmp = line.getString();
yamlHeader.add(tmp);
if (tmp.contains(":")) {
final String[] split = tmp.split(":");
metadata.put(split[0].trim(), split[1].trim());
}
} while (true);
// Skip the second separator
line = source.readLine();
}

}
return line;
}

private boolean isSeparator(StringLocated line) {
return line.getString().equals("---");
}

public Map<String, String> getMetadata() {
return Collections.unmodifiableMap(metadata);
}

}
38 changes: 9 additions & 29 deletions src/net/sourceforge/plantuml/theme/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,50 +36,30 @@
package net.sourceforge.plantuml.theme;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sourceforge.plantuml.preproc.ReadLine;
import net.sourceforge.plantuml.preproc.ReadLineWithYamlHeader;
import net.sourceforge.plantuml.text.StringLocated;

public class Theme {

private final ReadLine source;

// Right now, the header is not used
// It will be used in next releases
private List<String> header;
private final ReadLineWithYamlHeader source;

public Theme(ReadLine source) {
this.source = source;
this.source = new ReadLineWithYamlHeader(source);
}

public StringLocated readLine() throws IOException {
StringLocated line = source.readLine();
if (header == null) {
// At this point, "line" is the very first line of the file
header = new ArrayList<String>();
if (isSeparator(line)) {
do {
line = source.readLine();
if (line == null || isSeparator(line))
break;
header.add(line.getString());
} while (true);
// Skip the second separator
line = source.readLine();
}

}
return line;
}

private boolean isSeparator(StringLocated line) {
return line.getString().equals("---");
return source.readLine();
}

public void close() throws IOException {
source.close();
}

public Map<String, String> getMetadata() {
return source.getMetadata();
}

}
24 changes: 17 additions & 7 deletions src/net/sourceforge/plantuml/tim/EaterForeach.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
package net.sourceforge.plantuml.tim;

import net.sourceforge.plantuml.json.JsonArray;
import net.sourceforge.plantuml.json.JsonObject;
import net.sourceforge.plantuml.json.JsonValue;
import net.sourceforge.plantuml.text.StringLocated;
import net.sourceforge.plantuml.tim.expression.TValue;

public class EaterForeach extends Eater {

private String varname;
private JsonArray jsonArray;
private JsonValue jsonValue;

public EaterForeach(StringLocated s) {
super(s);
Expand All @@ -57,22 +59,30 @@ public void analyze(TContext context, TMemory memory) throws EaterException {
checkAndEatChar("in");
skipSpaces();
final TValue value = eatExpression(context, memory);
this.jsonArray = (JsonArray) value.toJson();
this.jsonValue = (JsonValue) value.toJson();
}

public boolean isSkip() {
if (this.jsonArray == null) {
if (this.jsonValue == null)
return true;
}
return this.jsonArray.size() == 0;

return size(jsonValue) == 0;
}

public static int size(JsonValue value) {
if (value instanceof JsonArray)
return ((JsonArray) value).size();
if (value instanceof JsonObject)
return ((JsonObject) value).size();
throw new IllegalArgumentException();
}

public final String getVarname() {
return varname;
}

public final JsonArray getJsonArray() {
return jsonArray;
public final JsonValue getJsonValue() {
return jsonValue;
}

}
31 changes: 20 additions & 11 deletions src/net/sourceforge/plantuml/tim/ExecutionContextForeach.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@
*/
package net.sourceforge.plantuml.tim;

import net.sourceforge.plantuml.json.Json;
import net.sourceforge.plantuml.json.JsonArray;
import net.sourceforge.plantuml.json.JsonObject;
import net.sourceforge.plantuml.json.JsonValue;
import net.sourceforge.plantuml.tim.iterator.CodePosition;

public class ExecutionContextForeach {

private final String varname;
private final JsonArray jsonArray;
private final JsonValue jsonValue;
private final CodePosition codePosition;
private boolean skipMe;
private int currentIndex;

private ExecutionContextForeach(String varname, JsonArray jsonArray, CodePosition codePosition) {
private ExecutionContextForeach(String varname, JsonValue jsonValue, CodePosition codePosition) {
this.varname = varname;
this.jsonArray = jsonArray;
this.jsonValue = jsonValue;
this.codePosition = codePosition;
}

public static ExecutionContextForeach fromValue(String varname, JsonArray jsonArray, CodePosition codePosition) {
return new ExecutionContextForeach(varname, jsonArray, codePosition);
public static ExecutionContextForeach fromValue(String varname, JsonValue jsonValue, CodePosition codePosition) {
return new ExecutionContextForeach(varname, jsonValue, codePosition);
}

public void skipMeNow() {
Expand All @@ -67,23 +70,29 @@ public CodePosition getStartForeach() {
return codePosition;
}

public final int currentIndex() {
return currentIndex;
public JsonValue currentValue() {
if (jsonValue instanceof JsonArray)
return ((JsonArray) jsonValue).get(currentIndex);
if (jsonValue instanceof JsonObject) {
final JsonObject tmp = (JsonObject) jsonValue;
return Json.value(tmp.names().get(currentIndex));
}
throw new IllegalStateException();
}

public final void inc() {
this.currentIndex++;
if (currentIndex >= jsonArray.size()) {
if (currentIndex >= EaterForeach.size(jsonValue))
this.skipMe = true;
}

}

public final String getVarname() {
return varname;
}

public final JsonArray getJsonArray() {
return jsonArray;
public final JsonValue getJsonValue() {
return jsonValue;
}

}
27 changes: 19 additions & 8 deletions src/net/sourceforge/plantuml/tim/TContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import net.sourceforge.plantuml.preproc.ReadLine;
import net.sourceforge.plantuml.preproc.ReadLineList;
import net.sourceforge.plantuml.preproc.ReadLineReader;
import net.sourceforge.plantuml.preproc.ReadLineWithYamlHeader;
import net.sourceforge.plantuml.preproc.StartDiagramExtractReader;
import net.sourceforge.plantuml.preproc.Sub;
import net.sourceforge.plantuml.preproc.UncommentReadLine;
Expand Down Expand Up @@ -89,6 +90,7 @@
import net.sourceforge.plantuml.tim.builtin.FunctionExists;
import net.sourceforge.plantuml.tim.builtin.GetAllStdlib;
import net.sourceforge.plantuml.tim.builtin.GetAllTheme;
import net.sourceforge.plantuml.tim.builtin.GetCurrentTheme;
import net.sourceforge.plantuml.tim.builtin.GetJsonKey;
import net.sourceforge.plantuml.tim.builtin.GetJsonType;
import net.sourceforge.plantuml.tim.builtin.GetStdlib;
Expand Down Expand Up @@ -193,6 +195,7 @@ private void addStandardFunctions(Defines defines) {
functionsSet.addFunction(new FunctionExists());
functionsSet.addFunction(new GetAllStdlib());
functionsSet.addFunction(new GetAllTheme());
functionsSet.addFunction(new GetCurrentTheme());
functionsSet.addFunction(new GetJsonKey());
functionsSet.addFunction(new GetJsonType());
functionsSet.addFunction(new GetStdlib());
Expand Down Expand Up @@ -633,17 +636,23 @@ private void executeIncludeDef(TMemory memory, StringLocated s) throws EaterExce
}
}

private Map<String, String> headerMetadata = Collections.emptyMap();

public Map<String, String> getHeaderMetadata() {
return headerMetadata;
}

private void executeTheme(TMemory memory, StringLocated s) throws EaterException {
final EaterTheme eater = new EaterTheme(s.getTrimmed(), importedFiles);
eater.analyze(this, memory);
final Theme reader = eater.getTheme();
if (reader == null)
final Theme theme = eater.getTheme();
if (theme == null)
throw new EaterException("No such theme " + eater.getName(), s);

try {
final List<StringLocated> body = new ArrayList<>();
do {
final StringLocated sl = reader.readLine();
final StringLocated sl = theme.readLine();
if (sl == null) {
executeLines(memory, body, null, false);
return;
Expand All @@ -654,8 +663,9 @@ private void executeTheme(TMemory memory, StringLocated s) throws EaterException
Logme.error(e);
throw new EaterException("Error reading theme " + e, s);
} finally {
this.headerMetadata = theme.getMetadata();
try {
reader.close();
theme.close();
} catch (IOException e) {
Logme.error(e);
}
Expand Down Expand Up @@ -713,9 +723,10 @@ private void executeInclude(TMemory memory, StringLocated s) throws EaterExcepti
filesUsedCurrent.add(f2);
}
}
if (reader != null) {
if (reader != null)
try {
final List<StringLocated> body = new ArrayList<>();
reader = new ReadLineWithYamlHeader(reader);
do {
final StringLocated sl = reader.readLine();
if (sl == null) {
Expand All @@ -729,18 +740,18 @@ private void executeInclude(TMemory memory, StringLocated s) throws EaterExcepti
this.importedFiles = saveImportedFiles;

}
}

} catch (IOException e) {
Logme.error(e);
throw new EaterException("cannot include " + e, s);
} finally {
if (reader != null) {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
Logme.error(e);
}
}

}

throw new EaterException("cannot include " + location, s);
Expand Down
Loading

0 comments on commit 50ded0a

Please sign in to comment.