2024-05-11 23:20:06 -07:00
using CefSharp ;
using CefSharp.WinForms ;
2024-05-15 16:20:04 -07:00
using System.Linq.Expressions ;
2024-05-11 23:20:06 -07:00
2024-05-11 16:00:45 -07:00
namespace MintHTML
{
public partial class Form1 : Form
{
2024-05-13 12:24:30 -07:00
string appdata = Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) ;
2024-05-12 16:05:39 -07:00
string markfile ;
string htmlfile = "<h1>Welcome to MintHTML</h1><p>Open a markdown file and press \"Render preview\" to see the output here.</p>" ;
string csssuffix = @ "
div {
font - family : monospace ;
background - color : # F3F3F3 ;
border : solid ;
border - width : 1 px
}
2024-05-14 00:40:35 -07:00
< / style >
< ul style = "" list - style - type : circle ; "" > ";
2024-05-12 16:05:39 -07:00
string css ;
// Custom functions
private void convert ( )
{
htmlfile = "" ;
using ( StringReader reader = new StringReader ( markfile ) )
{
string line ;
while ( ( line = reader . ReadLine ( ) ) ! = null )
{
2024-05-15 16:20:04 -07:00
try
{
2024-05-13 12:24:30 -07:00
// Header 1
2024-05-15 16:20:04 -07:00
if ( line [ 1. . 2 ] = = "# " )
2024-05-12 16:05:39 -07:00
{
line = "<h1>" + line [ 1. . ] + "</h1>" ;
}
2024-05-13 12:24:30 -07:00
// Header 2
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 3 ] = = "## " )
2024-05-13 12:24:30 -07:00
{
line = "<h2>" + line [ 1. . ] + "</h2>" ;
}
// Header 3
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 4 ] = = "### " )
2024-05-13 12:24:30 -07:00
{
line = "<h3>" + line [ 1. . ] + "</h3>" ;
}
// Header 4
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 5 ] = = "#### " )
2024-05-13 12:24:30 -07:00
{
line = "<h4>" + line [ 1. . ] + "</h4>" ;
}
// Header 5
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 6 ] = = "##### " )
2024-05-13 12:24:30 -07:00
{
line = "<h5>" + line [ 1. . ] + "</h5>" ;
}
// Header 6
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 7 ] = = "###### " )
2024-05-13 12:24:30 -07:00
{
line = "<h6>" + line [ 1. . ] + "</h6>" ;
}
2024-05-14 00:40:35 -07:00
// Bullet point
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 2 ] = = "* " )
2024-05-14 00:40:35 -07:00
{
line = "<li>" + line [ 1. . ] + "</li>" ;
}
2024-05-15 16:20:04 -07:00
else if ( line [ 1. . 2 ] = = "- " )
2024-05-14 00:40:35 -07:00
{
line = "<li>" + line [ 1. . ] + "</li>" ;
2024-05-15 16:20:04 -07:00
} } catch { }
2024-05-13 12:24:30 -07:00
// Code block
2024-05-15 16:20:04 -07:00
if ( line . Contains ( "`" ) )
2024-05-12 16:05:39 -07:00
{
line = "<div>" + line [ 1. . ^ 1 ] + "</div>" ;
}
2024-05-13 12:24:30 -07:00
// Everything else (Possibly paragraph or HTML code)
2024-05-12 16:05:39 -07:00
else
{
line = line + "<br>" ;
}
htmlfile + = line ;
}
}
}
// Custom functions end
2024-05-13 12:24:30 -07:00
2024-05-11 16:00:45 -07:00
public Form1 ( )
{
2024-05-13 12:24:30 -07:00
if ( File . Exists ( appdata + "/SweeZero/MintHTML/RootCache/lockfile" ) )
{
2024-05-14 00:40:35 -07:00
if ( MessageBox . Show ( "There is another instance of MintHTML running. If there isn't, press Yes to try opening anyways." , "Cef Warning" , MessageBoxButtons . YesNo , MessageBoxIcon . Warning ) = = DialogResult . Yes )
2024-05-13 12:24:30 -07:00
{
try
{
File . Delete ( appdata + "/SweeZero/MintHTML/RootCache/lockfile" ) ;
2024-05-14 00:40:35 -07:00
}
catch ( Exception ex )
{
2024-05-13 12:24:30 -07:00
MessageBox . Show ( ex . Message , "Cef Error (fatal)" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
}
}
var settings = new CefSettings ( )
{
RootCachePath = appdata + "/SweeZero/MintHTML/RootCache" ,
} ;
2024-05-11 16:00:45 -07:00
InitializeComponent ( ) ;
2024-05-14 00:40:35 -07:00
Cef . Initialize ( settings ) ;
2024-05-13 12:24:30 -07:00
css = @ "<style>
html {
font - family : sans - serif
} " + csssuffix;
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
2024-05-11 16:00:45 -07:00
}
2024-05-11 16:12:18 -07:00
2024-05-11 16:41:08 -07:00
private void aboutToolStripMenuItem_Click ( object sender , EventArgs e )
{
2024-05-15 16:20:04 -07:00
MessageBox . Show ( "MintHTML is a simple Markdown converter and renderer made by SweeZero's founder \"Swee.\"" , "About" , MessageBoxButtons . OK ) ;
2024-05-11 16:41:08 -07:00
}
private void button2_Click ( object sender , EventArgs e )
{
Opener . ShowDialog ( ) ;
textBox2 . Text = Opener . FileName ;
2024-05-12 16:05:39 -07:00
if ( textBox2 . Text . Length > 0 )
{
markfile = File . ReadAllText ( textBox2 . Text ) ;
convert ( ) ;
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
}
}
private void Form1_Load ( object sender , EventArgs e )
{
2024-05-11 16:41:08 -07:00
}
2024-05-12 16:05:39 -07:00
private void button3_Click ( object sender , EventArgs e )
2024-05-11 16:41:08 -07:00
{
2024-05-12 16:42:49 -07:00
Saver . FileName = textBox2 . Text . Split ( "\\" ) . Last ( ) [ 0. . ^ 3 ] + ".html" ;
DialogResult result = Saver . ShowDialog ( ) ;
if ( result = = DialogResult . OK )
{
2024-05-14 00:40:35 -07:00
if ( MessageBox . Show ( "Do you want to keep the current CSS settings?" , "Converter" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) = = DialogResult . Yes )
2024-05-12 16:42:49 -07:00
{
convert ( ) ;
2024-05-14 00:40:35 -07:00
File . WriteAllText ( Saver . FileName , css + htmlfile ) ;
}
else
{
2024-05-12 16:42:49 -07:00
convert ( ) ;
File . WriteAllText ( Saver . FileName , htmlfile ) ;
}
2024-05-14 00:40:35 -07:00
MessageBox . Show ( "Save complete." , "Converter" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2024-05-12 16:42:49 -07:00
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
}
2024-05-11 23:20:06 -07:00
}
2024-05-12 16:05:39 -07:00
private void Form1_FormClosed ( object sender , FormClosedEventArgs e )
2024-05-11 23:20:06 -07:00
{
2024-05-12 16:05:39 -07:00
Cef . Shutdown ( ) ;
}
2024-05-11 23:20:06 -07:00
2024-05-12 16:05:39 -07:00
private void chromiumWebBrowser1_LoadingStateChanged ( object sender , LoadingStateChangedEventArgs e )
{
this . Invoke ( new Action ( ( ) = > progressBar1 . Visible = chromiumWebBrowser1 . IsLoading ) ) ;
2024-05-14 00:40:35 -07:00
this . Invoke ( new Action ( ( ) = > openDevToolsToolStripMenuItem . Enabled = ! chromiumWebBrowser1 . IsLoading ) ) ;
2024-05-11 23:20:06 -07:00
}
2024-05-12 16:05:39 -07:00
private void openDevToolsToolStripMenuItem_Click_1 ( object sender , EventArgs e )
2024-05-11 23:20:06 -07:00
{
2024-05-12 16:05:39 -07:00
chromiumWebBrowser1 . ShowDevTools ( ) ;
2024-05-11 23:20:06 -07:00
}
2024-05-12 16:05:39 -07:00
private void button4_Click ( object sender , EventArgs e )
{
try
{
markfile = File . ReadAllText ( textBox2 . Text ) ;
convert ( ) ;
}
catch ( Exception ex )
{
MessageBox . Show ( ex . Message , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
}
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
}
private void textBox2_TextChanged ( object sender , EventArgs e )
{
button3 . Enabled = textBox2 . Text . Length > 0 ;
button4 . Enabled = textBox2 . Text . Length > 0 ;
}
private void serifToolStripMenuItem_Click ( object sender , EventArgs e )
{
css = @ "<style>
html {
font - family : serif
} " + csssuffix;
sansSerifToolStripMenuItem . Checked = false ;
forceMonospaceToolStripMenuItem . Checked = false ;
serifToolStripMenuItem . Checked = true ;
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
}
private void groupBox4_Enter ( object sender , EventArgs e )
2024-05-11 23:20:06 -07:00
{
2024-05-11 16:41:08 -07:00
}
2024-05-12 16:05:39 -07:00
private void sansSerifToolStripMenuItem_Click ( object sender , EventArgs e )
{
css = @ "<style>
html {
font - family : sans - serif
} " + csssuffix;
sansSerifToolStripMenuItem . Checked = true ;
forceMonospaceToolStripMenuItem . Checked = false ;
serifToolStripMenuItem . Checked = false ;
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
}
private void forceMonospaceToolStripMenuItem_Click ( object sender , EventArgs e )
{
css = @ "<style>
html {
font - family : monospace
} " + csssuffix;
sansSerifToolStripMenuItem . Checked = false ;
forceMonospaceToolStripMenuItem . Checked = true ;
serifToolStripMenuItem . Checked = false ;
chromiumWebBrowser1 . LoadHtml ( css + htmlfile ) ;
}
2024-05-15 16:20:04 -07:00
private void exitToolStripMenuItem_Click ( object sender , EventArgs e )
{
Application . Exit ( ) ;
}
2024-05-11 16:00:45 -07:00
}
}