From 28983460d72ee270ced91070bc11d6de6111b5b9 Mon Sep 17 00:00:00 2001 From: matthew16550 Date: Mon, 10 May 2021 12:36:09 +1000 Subject: [PATCH] Add "help themes" command. And ThemeUtils class. --- .../plantuml/help/CommandHelp.java | 1 + .../plantuml/help/CommandHelpTheme.java | 85 +++++++++++++++++++ .../plantuml/help/HelpFactory.java | 1 + .../plantuml/preproc2/PreprocessorUtils.java | 11 --- .../plantuml/theme/ThemeUtils.java | 79 +++++++++++++++++ .../sourceforge/plantuml/tim/TContext.java | 3 +- .../sourceforge/plantuml/help/HelpTest.java | 19 +++++ .../sourceforge/plantuml/test/TestUtils.java | 25 ++++++ 8 files changed, 212 insertions(+), 12 deletions(-) create mode 100644 src/net/sourceforge/plantuml/help/CommandHelpTheme.java create mode 100644 src/net/sourceforge/plantuml/theme/ThemeUtils.java create mode 100644 test/net/sourceforge/plantuml/help/HelpTest.java diff --git a/src/net/sourceforge/plantuml/help/CommandHelp.java b/src/net/sourceforge/plantuml/help/CommandHelp.java index 8f18e644c..1babcc96d 100644 --- a/src/net/sourceforge/plantuml/help/CommandHelp.java +++ b/src/net/sourceforge/plantuml/help/CommandHelp.java @@ -69,6 +69,7 @@ public class CommandHelp extends SingleLineCommand2 { diagram.add("* help colors"); diagram.add("* help font"); diagram.add("* help skinparams"); + diagram.add("* help themes"); return CommandExecutionResult.ok(); } diff --git a/src/net/sourceforge/plantuml/help/CommandHelpTheme.java b/src/net/sourceforge/plantuml/help/CommandHelpTheme.java new file mode 100644 index 000000000..b9aad1b49 --- /dev/null +++ b/src/net/sourceforge/plantuml/help/CommandHelpTheme.java @@ -0,0 +1,85 @@ +/* ======================================================================== + * PlantUML : a free UML diagram generator + * ======================================================================== + * + * (C) Copyright 2009-2021, Arnaud Roques + * + * Project Info: http://plantuml.com + * + * If you like this project or if you find it useful, you can support us at: + * + * http://plantuml.com/patreon (only 1$ per month!) + * http://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: Matthew Leather + * + * + */ +package net.sourceforge.plantuml.help; + +import java.io.IOException; + +import net.sourceforge.plantuml.LineLocation; +import net.sourceforge.plantuml.Log; +import net.sourceforge.plantuml.command.CommandExecutionResult; +import net.sourceforge.plantuml.command.SingleLineCommand2; +import net.sourceforge.plantuml.command.regex.IRegex; +import net.sourceforge.plantuml.command.regex.RegexConcat; +import net.sourceforge.plantuml.command.regex.RegexLeaf; +import net.sourceforge.plantuml.command.regex.RegexResult; +import net.sourceforge.plantuml.theme.ThemeUtils; + +public class CommandHelpTheme extends SingleLineCommand2 { + + public CommandHelpTheme() { + super(getRegexConcat()); + } + + static IRegex getRegexConcat() { + return RegexConcat.build(CommandHelpTheme.class.getName(), RegexLeaf.start(), // + new RegexLeaf("help"), // + RegexLeaf.spaceOneOrMore(), // + new RegexLeaf("themes?"), RegexLeaf.end()); + } + + @Override + protected CommandExecutionResult executeArg(Help diagram, LineLocation location, RegexResult arg) { + diagram.add("Help on themes"); + diagram.add(" "); + diagram.add("The code of this command is located in net.sourceforge.plantuml.help package."); + diagram.add("You may improve it on https://github.com/plantuml/plantuml/tree/master/src/net/sourceforge/plantuml/help"); + diagram.add(" "); + diagram.add(" The possible themes are :"); + + try { + for (String theme : ThemeUtils.getAllThemeNames()) { + diagram.add("* " + theme); + } + } catch (IOException e) { + final String message = "Unexpected error listing themes: " + e.getMessage(); + Log.error(message); + e.printStackTrace(); + return CommandExecutionResult.error(message); + } + + return CommandExecutionResult.ok(); + } +} diff --git a/src/net/sourceforge/plantuml/help/HelpFactory.java b/src/net/sourceforge/plantuml/help/HelpFactory.java index da0220382..ccc736aad 100644 --- a/src/net/sourceforge/plantuml/help/HelpFactory.java +++ b/src/net/sourceforge/plantuml/help/HelpFactory.java @@ -59,6 +59,7 @@ public class HelpFactory extends PSystemCommandFactory { cmds.add(new CommandHelpKeyword()); cmds.add(new CommandHelpSkinparam()); cmds.add(new CommandHelpType()); + cmds.add(new CommandHelpTheme()); return cmds; } diff --git a/src/net/sourceforge/plantuml/preproc2/PreprocessorUtils.java b/src/net/sourceforge/plantuml/preproc2/PreprocessorUtils.java index efd217718..12167ada2 100644 --- a/src/net/sourceforge/plantuml/preproc2/PreprocessorUtils.java +++ b/src/net/sourceforge/plantuml/preproc2/PreprocessorUtils.java @@ -90,17 +90,6 @@ public class PreprocessorUtils { return result; } - public static ReadLine getReaderTheme(StringLocated s, String filename) { - Log.info("Loading theme " + filename); - final String res = "/themes/puml-theme-" + filename + ".puml"; - final String description = "<" + res + ">"; - final InputStream is = Stdlib.class.getResourceAsStream(res); - if (is == null) { - return null; - } - return ReadLineReader.create(new InputStreamReader(is), description); - } - public static ReadLine getReaderStdlibInclude(StringLocated s, String filename) { Log.info("Loading sdlib " + filename); InputStream is = getStdlibInputStream(filename); diff --git a/src/net/sourceforge/plantuml/theme/ThemeUtils.java b/src/net/sourceforge/plantuml/theme/ThemeUtils.java new file mode 100644 index 000000000..0ad92c54f --- /dev/null +++ b/src/net/sourceforge/plantuml/theme/ThemeUtils.java @@ -0,0 +1,79 @@ +/* ======================================================================== + * PlantUML : a free UML diagram generator + * ======================================================================== + * + * (C) Copyright 2009-2021, Arnaud Roques + * + * Project Info: http://plantuml.com + * + * If you like this project or if you find it useful, you can support us at: + * + * http://plantuml.com/patreon (only 1$ per month!) + * http://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. + */ +package net.sourceforge.plantuml.theme; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +import net.sourceforge.plantuml.Log; +import net.sourceforge.plantuml.StringLocated; +import net.sourceforge.plantuml.preproc.ReadLine; +import net.sourceforge.plantuml.preproc.ReadLineReader; +import net.sourceforge.plantuml.preproc.Stdlib; +import net.sourceforge.plantuml.sprite.RessourcesUtils; + +public class ThemeUtils { + + private static final String THEME_FILE_PREFIX = "puml-theme-"; + + private static final String THEME_FILE_SUFFIX = ".puml"; + + private static final String THEME_PATH = "themes"; + + public static List getAllThemeNames() throws IOException { + final Collection filenames = Objects.requireNonNull(RessourcesUtils.getJarFile(THEME_PATH, false)); + final List result = new ArrayList<>(); + for (String f : filenames) { + if (f.startsWith(THEME_FILE_PREFIX) && f.endsWith(THEME_FILE_SUFFIX)) { + result.add(f.substring(THEME_FILE_PREFIX.length(), f.length() - THEME_FILE_SUFFIX.length())); + } + } + Collections.sort(result); + return result; + } + + public static ReadLine getReaderTheme(StringLocated s, String filename) { + Log.info("Loading theme " + filename); + final String res = "/" + THEME_PATH + "/" + THEME_FILE_PREFIX + filename + THEME_FILE_SUFFIX; + final String description = "<" + res + ">"; + final InputStream is = Stdlib.class.getResourceAsStream(res); + if (is == null) { + return null; + } + return ReadLineReader.create(new InputStreamReader(is), description); + } +} diff --git a/src/net/sourceforge/plantuml/tim/TContext.java b/src/net/sourceforge/plantuml/tim/TContext.java index 5138874d7..003cdd57a 100644 --- a/src/net/sourceforge/plantuml/tim/TContext.java +++ b/src/net/sourceforge/plantuml/tim/TContext.java @@ -64,6 +64,7 @@ import net.sourceforge.plantuml.preproc2.PreprocessorIncludeStrategy; import net.sourceforge.plantuml.preproc2.PreprocessorUtils; import net.sourceforge.plantuml.security.SFile; import net.sourceforge.plantuml.security.SURL; +import net.sourceforge.plantuml.theme.ThemeUtils; import net.sourceforge.plantuml.tim.expression.Knowledge; import net.sourceforge.plantuml.tim.expression.TValue; import net.sourceforge.plantuml.tim.iterator.CodeIterator; @@ -559,7 +560,7 @@ public class TContext { final EaterTheme include = new EaterTheme(s.getTrimmed()); include.analyze(this, memory); final String location = include.getLocation(); - final ReadLine reader = PreprocessorUtils.getReaderTheme(s, location); + final ReadLine reader = ThemeUtils.getReaderTheme(s, location); if (reader == null) { throw EaterException.located("No such theme " + location); } diff --git a/test/net/sourceforge/plantuml/help/HelpTest.java b/test/net/sourceforge/plantuml/help/HelpTest.java new file mode 100644 index 000000000..f014cca0b --- /dev/null +++ b/test/net/sourceforge/plantuml/help/HelpTest.java @@ -0,0 +1,19 @@ +package net.sourceforge.plantuml.help; + +import static net.sourceforge.plantuml.test.TestUtils.renderUmlAsUnicode; +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class HelpTest { + + @Test + public void test_help_themes() throws Exception { + + final String output = renderUmlAsUnicode("help themes"); + + assertThat(output) + .startsWith("Help on themes") + .contains("bluegray", "hacker"); + } +} diff --git a/test/net/sourceforge/plantuml/test/TestUtils.java b/test/net/sourceforge/plantuml/test/TestUtils.java index 2506ddf4a..9aedf4eaa 100644 --- a/test/net/sourceforge/plantuml/test/TestUtils.java +++ b/test/net/sourceforge/plantuml/test/TestUtils.java @@ -2,12 +2,37 @@ package net.sourceforge.plantuml.test; import static java.nio.charset.StandardCharsets.UTF_8; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import net.sourceforge.plantuml.FileFormat; +import net.sourceforge.plantuml.FileFormatOption; +import net.sourceforge.plantuml.Option; +import net.sourceforge.plantuml.SourceStringReader; + public class TestUtils { + public static String renderAsUnicode(String source, String... options) throws Exception { + + final Option option = new Option(options); + option.setFileFormatOption(new FileFormatOption(FileFormat.UTXT)); + + final SourceStringReader ssr = new SourceStringReader(option.getDefaultDefines(), source, "UTF-8", option.getConfig()); + + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + + ssr.getBlocks().get(0).getDiagram().exportDiagram(os, 0, option.getFileFormatOption()); + + return new String(os.toByteArray(), UTF_8); + } + + public static String renderUmlAsUnicode(String source, String... options) throws Exception { + + return renderAsUnicode("@startuml\n" + source + "\n@enduml\n", options); + } + public static void writeUtf8File(Path path, String string) throws IOException { Files.createDirectories(path.getParent());