Skip to content

Commit

Permalink
#398 Create user guide generator
Browse files Browse the repository at this point in the history
  • Loading branch information
prmr committed Dec 5, 2020
1 parent 2bfdfaf commit 73cc054
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/user-guide-template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# User Guide

This page contains the complete list of "tips" available through JetUML's "Tip of the Day" dialog. Click on an entry to expand with details.

<div id="body">
$TEXT$
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<style>
.collapsible
{
background-color: #DCDCDC;
color: black;
cursor: pointer;
padding: 10px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.collapsible:hover
{
background-color: #8e8e8e;
}
.content
{
padding: 0px 10px;
overflow: hidden;
background-color: #f1f1f1;
max-height: 0;
transition: max-height 0.2s ease-out;
}
</style>

<!-- Source: https://www.w3schools.com/ -->
<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
</script>
102 changes: 102 additions & 0 deletions src/ca/mcgill/cs/jetuml/gui/tips/UserGuideGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ca.mcgill.cs.jetuml.gui.tips;

import static ca.mcgill.cs.jetuml.application.ApplicationResources.RESOURCES;
import static ca.mcgill.cs.jetuml.gui.tips.TipLoader.loadTip;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.lines;
import static java.nio.file.Files.write;
import static java.util.stream.Collectors.joining;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

import ca.mcgill.cs.jetuml.gui.tips.TipLoader.Tip;

/**
* Script to generate the user guide by collecting all the tips of the day in a single page.
* The user guide is created by expanding a template md file with HTML content
* to show all the tips in collapsible elements.
*/
public final class UserGuideGenerator
{
private static final Path INPUT_FILE = Paths.get("docs", "user-guide-template.txt");
private static final Path OUTPUT_FILE = Paths.get("docs", "user-guide.md");

private static final String PLACEHOLDER = "$TEXT$";
private static final String TEMPLATE_BUTTON = "<button class=\"collapsible\">$TEXT$</button>";
private static final String TEMPLATE_DIV_OPEN = "<div class=\"content\">";
private static final String TEMPLATE_TEXT = "<p>$TEXT$</p>";
private static final String TEMPLATE_IMAGE = "<img src=\"../tipdata/tip_images/$TEXT$\">";
private static final String TEMPLATE_DIV_CLOSE = "</div>";

private UserGuideGenerator() {}

/**
* Run without arguments.
*
* @param pArgs Not used.
*/
public static void main(String[] pArgs) throws IOException
{
String template = lines(INPUT_FILE, UTF_8).collect(joining("\n"));
write(OUTPUT_FILE, template.replace(PLACEHOLDER, tipsAsHtml()).getBytes(UTF_8));
System.out.println("The User Guide was generated sucessfully.");
}

/*
* Creates an html representation of all available tips.
*/
private static String tipsAsHtml()
{
List<String> tips = new ArrayList<>();
for( int tipNumber = 1; tipNumber <= numberOfTips(); tipNumber++ )
{
tips.add(toHtml(loadTip(tipNumber)));
}
return tips.stream().collect(joining("\n"));
}

/*
* Obtains the number of tips for the application's properties.
*/
private static int numberOfTips()
{
return Integer.parseInt(RESOURCES.getString("tips.quantity"));
}

/*
* Creates an html representation of pTip suitable for display in the user guide.
*/
private static String toHtml(Tip pTip)
{
StringJoiner html = new StringJoiner("\n");
html.add(TEMPLATE_BUTTON.replace(PLACEHOLDER, pTip.getTitle()));
html.add(TEMPLATE_DIV_OPEN);
for( TipElement element : pTip.getElements() )
{
html.add(toHtml(element));
}
html.add(TEMPLATE_DIV_CLOSE);

return html.toString();
}

/*
* Creates an html representation of pTipElement suitable for display in the user guide.
*/
private static String toHtml(TipElement pTipElement)
{
if( pTipElement.getMedia() == Media.TEXT )
{
return TEMPLATE_TEXT.replace(PLACEHOLDER, pTipElement.getContent());
}
else
{
return TEMPLATE_IMAGE.replace(PLACEHOLDER, pTipElement.getContent());
}
}
}

0 comments on commit 73cc054

Please sign in to comment.