Skip to content

Commit

Permalink
finish adding Describe v1.0 functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Chernev committed Dec 12, 2023
1 parent ce6ea9a commit 0cefb27
Show file tree
Hide file tree
Showing 72 changed files with 771 additions and 713 deletions.
5 changes: 0 additions & 5 deletions @DescribeCompilerAPI/Compiler/Compiler/VerbosityLow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,6 @@ private bool parseFile_LowVerbosity(FileInfo fileInfo, DescribeUnfold unfold)
string source = "";
try
{
if (fileInfo.FullName.Contains("technologiesAndInventions.ds"))
{
bool lll = false;
}

source = File.ReadAllText(fileInfo.FullName);
source = _Preprocessor.ProcessSource(source);
if (source.Length == 0)
Expand Down
2 changes: 1 addition & 1 deletion @DescribeCompilerAPI/DescribeCompilerAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<Compile Include="Compiler\Preprocessors\PreprocessorForDescribe08.cs" />
<Compile Include="Compiler\Preprocessors\PreprocessorForDescribe07.cs" />
<Compile Include="Compiler\Preprocessors\PreprocessorForDescribe06.cs" />
<Compile Include="Translators\CharacterDictionaries.cs" />
<Compile Include="Translators\Dictionaries\CharacterDictionaries.cs" />
<Compile Include="Translators\JsonTranslator.cs" />
<Compile Include="Translators\DescribeTranslator.cs" />
<Compile Include="Translators\HtmlTranslator.cs" />
Expand Down
42 changes: 6 additions & 36 deletions @DescribeCompilerAPI/Translators/DescribeTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@ namespace DescribeCompiler.Translators
{
public abstract class DescribeTranslator
{
/// <summary>
/// Wether the Translator makes use of template files.
/// </summary>
public abstract bool USES_TEMPLATES
{
get;
}

/// <summary>
/// Wether there are inbuilt templates for the translator.
/// </summary>
public abstract bool HAS_INBUILT_TEMPLATES
{
get;
}

/// <summary>
/// The default name of the templates for the translator.
/// If HAS_INBUILT_TEMPLATES then this should be the name of some
/// of the inbuilt templates in folder "Templates"
/// </summary>
public abstract string DEFAULT_TEMPLATES_NAME
{
get;
}

/// <summary>
/// This is meant as a failsafe - if false then the
/// translation process should not be allowed to start.
Expand All @@ -46,21 +20,17 @@ public abstract bool IsInitialized
}


/// <summary>
/// Translade an unfold structure. This is the main method.
/// </summary>
/// <param name="u">The unfold structure to translate.</param>
/// <returns>The resulted string in the target language.</returns>
public abstract string TranslateUnfold(DescribeUnfold u);
public abstract bool LoadExternalTemplates(string path);
public abstract bool LoadInternalTemplates(string name);

}
}
// After we have parsed our files and optimized the resulting parse tree to content in an Unfold
// structure, we might want to translate this unfold structure to a desired language (like HTML,
// or JSON e.g.) This is where Translators come into play.
//
// Previously (before v0.9.2) translators were a part of the compilation process itself, which
// limited flexibility and made implementation of custom translators harder.
//
// Translators might (and will in many cases) make use of "translation templates". Those are
// collections of source code files containing placeholder text where information will be inserted.
// There are inbuilt templates it this project (as embedded resources - folder "Templates"), and
// there are inbuilt translators that will use those. You can use those templates in building
// your own translators, and you can provide an inbuilt translator with external templates.
// limited flexibility and made implementation of custom translators harder.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;


namespace DescribeCompiler.Translators
{
Expand Down Expand Up @@ -203,6 +200,5 @@ public static class CharacterDictionariesHtml
}
}
// http://xahlee.info/comp/unicode_circled_numbers.html
//
// Those are simply dictionaries that map normal characters or integer numbers to Unicode escape sequences.
// Those can then be used when writing translators.
143 changes: 14 additions & 129 deletions @DescribeCompilerAPI/Translators/HtmlTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ namespace DescribeCompiler.Translators
{
public class HtmlTranslator : DescribeTranslator
{
public override bool USES_TEMPLATES
{
get { return true; }
}
public override bool HAS_INBUILT_TEMPLATES
{
get { return true; }
}
public override string DEFAULT_TEMPLATES_NAME
{
get { return "HTML_PARACORD"; }
}
public override bool IsInitialized
{
get;
Expand All @@ -34,9 +22,7 @@ public override bool IsInitialized


//templates
public bool selectInbuiltTemplate = true;
public string selectedTemplate = null;

const string templatesFolderName = "HTML_PARACORD";
static string pageTemplate;
static string rootTemplate;
static string itemTemplate;
Expand Down Expand Up @@ -64,130 +50,29 @@ public HtmlTranslator()
//try to initialize templates
try
{
if (!USES_TEMPLATES)
{
IsInitialized = true;
LogInfo("Translator initialized - not using templates");
}
else if (HAS_INBUILT_TEMPLATES)
{
string n = DEFAULT_TEMPLATES_NAME;
pageTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Page");
rootTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Root");
coloredProductionTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ProductionColored");
productionTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Production");
itemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Item");
emptyItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemEmpty");
nlcommentItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemCommentNl");
commentItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemComment");
coloredItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemColored");
linkTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Link");

LogInfo("Translator initialized - using template \"" + n + "\"");
selectInbuiltTemplate = true;
selectedTemplate = n;
IsInitialized = true;
}
else
{
LogInfo("Translator NOT initialized - Must further load templates from folder before using.");
selectInbuiltTemplate = false;
IsInitialized = false;
}
}
catch (Exception ex)
{
IsInitialized = false;
LogError("Fatal error: " + ex.Message);
}
}

/// <summary>
/// Load templates from an external folder.
/// </summary>
/// <param name="path">The path to the desired templates folder</param>
/// <returns>True if successful</returns>
public override bool LoadExternalTemplates(string path)
{
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
if (directoryInfo.Exists)
{
FileInfo[] fs = directoryInfo.GetFiles();
foreach (FileInfo finfo in fs)
{
// make sure that "ItemEmpty" or "ItemComment" and all other
// that start with Item are before "Item"
if (finfo.Name.StartsWith("Page")) pageTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("Root")) rootTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("ProductionColored")) coloredProductionTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("Production")) productionTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("ItemEmpty")) emptyItemTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("ItemCommentNl")) nlcommentItemTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("ItemComment")) commentItemTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("ItemColored")) coloredItemTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("Item")) itemTemplate = File.ReadAllText(finfo.FullName);
else if (finfo.Name.StartsWith("Link")) linkTemplate = File.ReadAllText(finfo.FullName);
}

LogInfo("Translator initialized - using external template \"" + path + "\"");
selectInbuiltTemplate = false;
selectedTemplate = path;
IsInitialized = true;
return true;
}
else
{
LogInfo("Translator Not initialized - external template path does not exist \"" + path + "\"");
selectInbuiltTemplate = false;
selectedTemplate = path;
IsInitialized = false;
return false;
}
}
catch (Exception ex)
{
IsInitialized = false;
LogError("Fatal error: " + ex.Message);
return false;
}
}

/// <summary>
/// Load templates from an internal folder of embedded resources.
/// </summary>
/// <param name="name">The name of the internal folder</param>
/// <returns>True if successful</returns>
public override bool LoadInternalTemplates(string name)
{
try
{
pageTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"Page");
rootTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"Root");
coloredProductionTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"ProductionColored");
productionTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"Production");
itemTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"Item");
emptyItemTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"ItemEmpty");
nlcommentItemTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"ItemCommentNl");
commentItemTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"ItemComment");
coloredItemTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"ItemColored");
linkTemplate = ResourceUtil.ExtractResourceByFileName_String(name, @"Link");
string n = templatesFolderName;
pageTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Page");
rootTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Root");
coloredProductionTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ProductionColored");
productionTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Production");
itemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Item");
emptyItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemEmpty");
nlcommentItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemCommentNl");
commentItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemComment");
coloredItemTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"ItemColored");
linkTemplate = ResourceUtil.ExtractResourceByFileName_String(n, @"Link");

LogInfo("Translator initialized - using template \"" + name + "\"");
selectInbuiltTemplate = true;
selectedTemplate = name;
LogInfo("Translator initialized - using template \"" + n + "\"");
IsInitialized = true;
return true;
}
catch (Exception ex)
{
IsInitialized = false;
LogError("Fatal error: " + ex.Message);
return false;
}
}


/// <summary>
/// Get html code from unfold
/// </summary>
Expand Down
Loading

0 comments on commit 0cefb27

Please sign in to comment.