1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-01 08:00:48 +00:00

Add "help themes" command. And ThemeUtils class.

This commit is contained in:
matthew16550 2021-05-10 12:36:09 +10:00
parent 7afaa0c743
commit 28983460d7
8 changed files with 212 additions and 12 deletions

View File

@ -69,6 +69,7 @@ public class CommandHelp extends SingleLineCommand2<Help> {
diagram.add("* help colors");
diagram.add("* help font");
diagram.add("* help skinparams");
diagram.add("* help themes");
return CommandExecutionResult.ok();
}

View File

@ -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<Help> {
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("<b>Help on themes");
diagram.add(" ");
diagram.add("The code of this command is located in <i>net.sourceforge.plantuml.help</i> package.");
diagram.add("You may improve it on <i>https://github.com/plantuml/plantuml/tree/master/src/net/sourceforge/plantuml/help</i>");
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();
}
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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<String> getAllThemeNames() throws IOException {
final Collection<String> filenames = Objects.requireNonNull(RessourcesUtils.getJarFile(THEME_PATH, false));
final List<String> 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);
}
}

View File

@ -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);
}

View File

@ -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");
}
}

View File

@ -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());