Skip to content

Commit

Permalink
String optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddiM8 committed Mar 15, 2019
1 parent 4bf5706 commit 2d68d8d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 56 deletions.
2 changes: 0 additions & 2 deletions Enums/OutputTypes.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
public enum OutputTypes
{
HtmlFull,
HtmlSpans,
HtmlDiv,
Json
}
51 changes: 13 additions & 38 deletions Generators/HtmlGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class HtmlGenerator
{
public string GenerateSpans(List<Sentence> sentences)
{
return GeneratePrefixedSpans(sentences, "");
}

private string GeneratePrefixedSpans(List<Sentence> sentences, string prefix = "")
{
string sentencesString = "";
foreach (var sentence in sentences)
sentencesString += prefix + GenerateSentence(sentence);

return sentencesString;
}
private static StringBuilder stringBuild = new StringBuilder();

public string GenerateFull(List<Sentence> sentences)
{
foreach (var sentence in sentences)
GenerateSentence(sentence);

string templatesDirectory = "Resources/";
return File.ReadAllText(templatesDirectory + "top.html") +
GenerateDiv(sentences) +
stringBuild.ToString() +
File.ReadAllText(templatesDirectory + "bottom.html");
}

public string GenerateDiv(List<Sentence> sentences)
{
string spans = GeneratePrefixedSpans(sentences, "\t");
return $"<div id='glossbox'>\n<span id='tooltip'></span><div id='tooltip-arrow'></div>\n{spans}\n</div>";
}

private string GenerateSentence(Sentence sentence)
private void GenerateSentence(Sentence sentence)
{
string sentenceString = "";
for (int i = 0; i < sentence.Words.Count; i++) {
sentenceString += GenerateWord(sentence.Words[i], i == 0);

if (i != sentence.Words.Count - 1) sentenceString += "\n";
else sentenceString += ".";
}
for (int s = 0; s < sentence.Words.Count; s++) {
var word = sentence.Words[s];
for (int w = 0; w < word.Morphemes.Count; w++)
stringBuild.Append(GenerateSpan(word.Morphemes[w], s == 0 && w == 0));

return sentenceString;
}

private string GenerateWord(Word word, bool firstWord = false)
{
string wordString = "";
for (int i = 0; i < word.Morphemes.Count; i++) {
wordString += GenerateSpan(word.Morphemes[i], firstWord && i == 0);
if (s != sentence.Words.Count - 1) stringBuild.Append("\n");
else stringBuild.Append(".");
}

return wordString;
}

private string GenerateSpan(Morpheme morpheme, bool firstWord = false)
Expand Down
18 changes: 4 additions & 14 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static void Main(string[] args)
{
List<Token> tokens = new Lexer().Lex(File.ReadAllText(inputFile));
var parse = new Parser().Parse(tokens);

string output = "";
output = OutputFunction(parse);
string fileName = Path.GetFileNameWithoutExtension(inputFile);
Expand Down Expand Up @@ -128,7 +127,7 @@ private static void ShowHelp()
{
Console.WriteLine("-=GlossVisualiser Help=-");
Console.WriteLine("-h, --help: Show help");
Console.WriteLine("-t: Output type (json, html, html-spans, html-div)");
Console.WriteLine("-t: Output type (json, html)");
Console.WriteLine("-o: Output method (console, file)");
Console.WriteLine("-ab, --add-abbreviation: [ABBREVIATION] [Color] [Value/Meaning]");
Console.WriteLine("-eb, --edit-abreviation: [ABBREVIATION] [Color] [Value/Meaning]");
Expand All @@ -139,12 +138,8 @@ private static Output GetOutputFunction(string input)
{
switch (input.ToLower())
{
case "html-full":
case "html":
return new Output(new HtmlGenerator().GenerateFull);
case "html-spans":
return new Output(new HtmlGenerator().GenerateSpans);
case "html-div":
return new Output(new HtmlGenerator().GenerateDiv);
case "json":
return new Output(new JsonGenerator().Generate);
default:
Expand All @@ -156,12 +151,8 @@ public static OutputTypes GetOutputType(string input)
{
switch (input.ToLower())
{
case "html-full":
case "html":
return OutputTypes.HtmlFull;
case "html-spans":
return OutputTypes.HtmlSpans;
case "html-div":
return OutputTypes.HtmlDiv;
case "json":
return OutputTypes.Json;
default:
Expand All @@ -184,10 +175,9 @@ private static OutputMethods GetOutputMethod(string input)

private static string OutputTypeToExtension(OutputTypes outputType)
{
// This will eventually have a few more.
switch (outputType)
{
case OutputTypes.HtmlSpans:
return ".html";
case OutputTypes.Json:
return ".json";
default:
Expand Down
3 changes: 2 additions & 1 deletion Resources/bottom.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<script src="script.js"></script>
</div>
<script src="script.js"></script>
</body>
</html>
4 changes: 3 additions & 1 deletion Resources/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ document.getElementById("glossbox").addEventListener('mouseover', function(evt)
var labels = evt.target.getAttribute("labels").split(' ');
var i;
for (i in labels) {
if (labels[i] != "")
if (labels[i] != "" && labels[i].toUpperCase() == labels[i])
tooltipText += "<br />" + abbreviations[labels[i]];
else if (labels[i] != "")
tooltipText += " " + labels[i];
}

tooltip.innerHTML = tooltipText;
Expand Down
3 changes: 3 additions & 0 deletions Resources/top.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="glossbox">
<span id="tooltip"></span><div id="tooltip-arrow"></div>

1 change: 1 addition & 0 deletions output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generators/HtmlGenerator.cs(7,19): error CS0246: The type or namespace name 'StringBuilder' could not be found (are you missing a using directive or an assembly reference?) [/home/paddi/Projects/GlossVisualiser/GlossVisualiser.csproj]

0 comments on commit 2d68d8d

Please sign in to comment.