Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jetspiking authored Feb 8, 2024
1 parent 662d5cd commit 0ac77b0
Show file tree
Hide file tree
Showing 22 changed files with 511 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Source/WebTeX.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebTeX", "WebTeX\WebTeX.csproj", "{783B063D-69A3-4782-A9D2-DE1DD77300A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{783B063D-69A3-4782-A9D2-DE1DD77300A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{783B063D-69A3-4782-A9D2-DE1DD77300A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{783B063D-69A3-4782-A9D2-DE1DD77300A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{783B063D-69A3-4782-A9D2-DE1DD77300A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {187DCBD6-1657-4F54-A9E8-D0E52D6C2200}
EndGlobalSection
EndGlobal
27 changes: 27 additions & 0 deletions Source/WebTeX/Core/CommandInvoker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Diagnostics;

namespace WebTeX.Core
{
public static class CommandInvoker
{
public static bool InvokeProcess(String processName, String processArgument, String? workingDirectory = null)
{
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(processName);
processStartInfo.Arguments = processArgument;
if (workingDirectory != null) processStartInfo.WorkingDirectory = workingDirectory;

Process _process = Process.Start(processStartInfo);
_process.WaitForExit();

return _process.ExitCode == 0;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
}
}
7 changes: 7 additions & 0 deletions Source/WebTeX/Core/EldynTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace WebTeX.Core
{
public abstract class EldynTemplate
{
public abstract TemplateInstruction GetInstruction();
}
}
24 changes: 24 additions & 0 deletions Source/WebTeX/Core/JITTeX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace WebTeX.Core
{
public class JITTeX
{
public WebTeXArgs Configuration;
public JITTeX(WebTeXArgs configuration)
{
Configuration = configuration;
}

public Boolean InvokeEldyn(String file)
{
String arguments = $"{Path.Combine(Configuration.RootDirectoryPath, Configuration.PdfOutputTemporaryDirectoryName,"eldyn.json")} {Path.Combine(Configuration.RootDirectoryPath, Configuration.EldynTemplateDirectoryName)} {Path.Combine(Configuration.RootDirectoryPath, Configuration.TeXServeDirectoryName,file+".tex")}";
return CommandInvoker.InvokeProcess(Configuration.EldynExecutablePath, arguments);
}

public String CompileTeXFile(String file)
{
String arguments = $"-output-directory=\"{Path.Combine(Configuration.RootDirectoryPath, Configuration.PdfOutputTemporaryDirectoryName)}\" -interaction=nonstopmode \"{Path.Combine(Configuration.RootDirectoryPath, Configuration.TeXServeDirectoryName, file + ".tex")}\"";
CommandInvoker.InvokeProcess(Configuration.PdfLaTeXExecutablePath, arguments, Path.Combine(Configuration.RootDirectoryPath, Configuration.TeXServeDirectoryName));
return Path.Combine(Configuration.RootDirectoryPath, Configuration.PdfOutputTemporaryDirectoryName, file + ".pdf");
}
}
}
33 changes: 33 additions & 0 deletions Source/WebTeX/Core/JsonManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebTeX.Core
{
public static class JsonManager
{
public static void SerializeToFile<T>(T obj, string path)
{
string? directoryPath = Path.GetDirectoryName(path);
if (directoryPath == null) return;
if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);
File.WriteAllText(path, JsonConvert.SerializeObject(obj));
}

public static T? DeserializeFromFile<T>(string path)
{
try
{
return JsonConvert.DeserializeObject<T>(File.ReadAllText(path));
}
catch(Exception e)
{
Console.WriteLine(e);
return default;
}
}
}
}
23 changes: 23 additions & 0 deletions Source/WebTeX/Core/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace WebTeX.Core
{
public class Program
{
public static void Main(String[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
WebApplication? app = builder.Build();
RequestHandler requestHandler = new(app);
}
}
}
26 changes: 26 additions & 0 deletions Source/WebTeX/Core/TeXPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace WebTeX.Core
{
public abstract class TeXPage
{
protected JITTeX _jITTeX;
protected String _template;
public TeXPage(JITTeX jITTeX, String template)
{
_jITTeX = jITTeX;
_template = template;
}
public async Task<Boolean> HandleRequest()
{
TemplateInstruction? demoRequest = await GetEldynJson();
if (demoRequest != null)
{
JsonManager.SerializeToFile(demoRequest, Path.Combine(_jITTeX.Configuration.RootDirectoryPath, _jITTeX.Configuration.PdfOutputTemporaryDirectoryName, "eldyn.json"));
_jITTeX.InvokeEldyn(_template);
return true;
}
else return false;
}

protected abstract Task<TemplateInstruction?> GetEldynJson();
}
}
34 changes: 34 additions & 0 deletions Source/WebTeX/Core/TemplateInstruction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebTeX.Core
{
public class TemplateInstruction
{
public string Template;
public TemplateValue[]? Properties;
public TemplateInstruction[]? Children;

public TemplateInstruction(string template, TemplateValue[]? properties, TemplateInstruction[]? children)
{
Template = template;
Properties = properties;
Children = children;
}
}

public class TemplateValue
{
public string Id;
public string Assign;

public TemplateValue(string id, string assign)
{
Id = id;
Assign = assign;
}
}
}
16 changes: 16 additions & 0 deletions Source/WebTeX/Core/WebTeXArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace WebTeX.Core
{
public class WebTeXArgs
{
public String RootDirectoryPath { get; set; } // Directory path to root directory.
public String TeXServeDirectoryName { get; set; } // Directory name for serving TeX.
public String PdfOutputTemporaryDirectoryName { get; set; } // Directory name for JIT files.
public String EldynTemplateDirectoryName { get; set; } // Directory name for ELDYN.
public String PdfLaTeXExecutablePath { get; set; } // Path to pdflatex compiler.
public String EldynExecutablePath { get; set; } // Path to ELDYN executable.
public String WebsiteUrl { get; set; } // Server URL for serving the PDF.
public String WebsitePort { get; set; } // Server port.
}
}
24 changes: 24 additions & 0 deletions Source/WebTeX/Pages/DemoErrorPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using WebTeX.Core;

namespace WebTeX.Pages
{
public class DemoErrorPage : TeXPage
{
public DemoErrorPage(JITTeX jITTeX, String template) : base(jITTeX, template)
{
}

protected override Task<TemplateInstruction?> GetEldynJson()
{
TemplateInstruction instruction = new DemoErrorPageJson(_jITTeX.Configuration.WebsiteUrl).GetInstruction();
if (instruction == null)
{
return Task.FromResult<TemplateInstruction?>(null);
}
else
{
return Task.FromResult<TemplateInstruction?>(instruction);
}
}
}
}
20 changes: 20 additions & 0 deletions Source/WebTeX/Pages/DemoErrorPageJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using WebTeX.Core;

namespace WebTeX.Pages
{
public class DemoErrorPageJson : EldynTemplate
{
private String _webUrl;
public DemoErrorPageJson(String webUrl)
{
_webUrl = webUrl;
}

public override TemplateInstruction GetInstruction()
{
TemplateInstruction[] demoTemplates = { new("DemoError", null, null) };
TemplateInstruction templateInstruction = new("Index", new TemplateValue[] { new TemplateValue("WebUrl", _webUrl) }, demoTemplates);
return templateInstruction;
}
}
}
48 changes: 48 additions & 0 deletions Source/WebTeX/Pages/DemoPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Newtonsoft.Json;
using System.Net;
using WebTeX.Core;

namespace WebTeX.Pages
{
public class DemoPage : TeXPage
{
public DemoPage(JITTeX jITTeX, String template) : base(jITTeX, template)
{

}

protected override async Task<TemplateInstruction?> GetEldynJson()
{
String url = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY";
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
String responseBody = await response.Content.ReadAsStringAsync();

dynamic data = JsonConvert.DeserializeObject(responseBody);
String title = data.title;
String imageUrl = data.url;
String explanation = data.explanation;

MemoryStream imageStream = new MemoryStream();
client.GetStreamAsync(imageUrl).Result.CopyTo(imageStream);

String imagePath = Path.Combine(this._jITTeX.Configuration.RootDirectoryPath, this._jITTeX.Configuration.TeXServeDirectoryName, "apod.jpg");
FileStream file = new FileStream(imagePath, FileMode.Create, System.IO.FileAccess.ReadWrite);
imageStream.WriteTo(file);
file.Close();

return new DemoPageJson(title, explanation, imageUrl, "apod.jpg", _jITTeX.Configuration.WebsiteUrl).GetInstruction();
}
}
catch (Exception e)
{
// Something went wrong upon retrieving the result from APOD.
return new DemoPageJson("Oops", "Something went wrong when retrieving the image from the Astronomy Picture of the Day API. Request count for DEMO\\_KEY might be exceeded.", "https://api.nasa.gov/", "example-image.png", _jITTeX.Configuration.WebsiteUrl).GetInstruction();
}
}
}
}
30 changes: 30 additions & 0 deletions Source/WebTeX/Pages/DemoPageJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using WebTeX.Core;

namespace WebTeX.Pages
{
public class DemoPageJson : EldynTemplate
{
private String _title;
private String _description;
private String _url;
private String _image;
private String _webUrl;

public DemoPageJson(String title, String description, String url, String image, String webUrl)
{
_title = title;
_description = description;
_url = url;
_image = image;
_webUrl = webUrl;
}

public override TemplateInstruction GetInstruction()
{
TemplateValue[] apodValues = { new TemplateValue("Title", _title), new TemplateValue("Explain", _description), new TemplateValue("Url", _url), new TemplateValue("Apod", _image) };
TemplateInstruction[] demoTemplates = { new("Demo", apodValues, null) };
TemplateInstruction templateInstruction = new("Index", new TemplateValue[] { new TemplateValue("WebUrl", _webUrl) }, demoTemplates);
return templateInstruction;
}
}
}
24 changes: 24 additions & 0 deletions Source/WebTeX/Pages/IndexPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using WebTeX.Core;

namespace WebTeX.Pages
{
public class IndexPage : TeXPage
{
public IndexPage(JITTeX jITTeX, String template) : base(jITTeX, template)
{
}

protected override Task<TemplateInstruction?> GetEldynJson()
{
TemplateInstruction instruction = new IndexPageJson(_jITTeX.Configuration.WebsiteUrl).GetInstruction();
if (instruction == null)
{
return Task.FromResult<TemplateInstruction?>(null);
}
else
{
return Task.FromResult<TemplateInstruction?>(instruction);
}
}
}
}
22 changes: 22 additions & 0 deletions Source/WebTeX/Pages/IndexPageJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using WebTeX.Core;
using static System.Net.Mime.MediaTypeNames;

namespace WebTeX.Pages
{
public class IndexPageJson : EldynTemplate
{
private String _webUrl;
public IndexPageJson(String webUrl)
{
_webUrl = webUrl;
}

public override TemplateInstruction GetInstruction()
{
TemplateValue[] templateValues = { new TemplateValue("WebUrl", _webUrl) };
TemplateInstruction[] demoTemplates = { new("Home", null, null) };
TemplateInstruction templateInstruction = new("Index", templateValues, demoTemplates);
return templateInstruction;
}
}
}
Loading

0 comments on commit 0ac77b0

Please sign in to comment.