Browse Source

Added syntax highlighting for pastes w/ backwards? support.

tags/3.0.0^2
Teknikode 1 year ago
parent
commit
43be6c4694

+ 35
- 3
Teknik/Areas/Paste/Views/Paste/Full.cshtml View File

@@ -4,14 +4,44 @@

<bundle src="css/paste.min.css" append-version="true"></bundle>

@{
string format = Model.Syntax;
string formatName = "Text";
bool useFormat = true;
bool autoDetect = false;

if (string.IsNullOrEmpty(format))
{
useFormat = false;
}
else if (format == "auto-detect")
{
formatName = "Auto Detect";
autoDetect = true;
}
else if (!HighlightHelper.Languages.ContainsKey(format))
{
useFormat = false;
}
else
{
formatName = HighlightHelper.Languages[format];
}
}

<script>
var format = '@format';
var useFormat = @(useFormat.ToString().ToLower());
var autoDetect = @(autoDetect.ToString().ToLower());
var createVaultURL = '@Url.SubRouteUrl("vault", "Vault.NewVaultFromService", new { type = "Paste" })';
var highlightWorkerSrc = '@Url.FullURL("~/js/highlight.worker.min.js")';
var highlightSrc = '@Url.FullURL("~/js/highlight.min.js")';
</script>

<div class="container">
<div class="row text-center">
<div class="col-sm-12 text-center">
<h2><b>@((string.IsNullOrEmpty(Model.Title)) ? "Untitled" : Model.Title)</b> <small>Posted on <time datetime="@Model.DatePosted.ToString("s")">@Model.DatePosted.ToString("dddd, MMMM d, yyyy") at @Model.DatePosted.ToString("h:mm:ss tt")</time> - Format: <b id="syntaxLanguage">@Model.Syntax</b></small></h2>
<h2><b>@((string.IsNullOrEmpty(Model.Title)) ? "Untitled" : Model.Title)</b> <small>Posted on <time datetime="@Model.DatePosted.ToString("s")">@Model.DatePosted.ToString("dddd, MMMM d, yyyy") at @Model.DatePosted.ToString("h:mm:ss tt")</time> - Format: <b id="syntaxLanguage">@(formatName)</b></small></h2>
</div>
</div>
<hr />
@@ -37,8 +67,10 @@
</div>
<br />
<div class="row">
<div class="col-sm-12">@Html.Raw(Model.Content)</div>
<div class="col-sm-12">
<pre><code id="code">@Html.Raw(Model.Content)</code></pre>
</div>
</div>
</div>

<bundle src="js/paste.min.js" append-version="true"></bundle>
<bundle src="js/paste.view.min.js" append-version="true"></bundle>

+ 10
- 26
Teknik/Areas/Paste/Views/Paste/Index.cshtml View File

@@ -1,7 +1,5 @@
@model Teknik.Areas.Paste.ViewModels.PasteCreateViewModel

@*@using Teknik.Pygments*@

<bundle src="css/paste.min.css" append-version="true"></bundle>

<div class="container">
@@ -12,7 +10,7 @@
</div>
<div class="row">
<div class="col-sm-12">
<form class="form-horizontal" name="editor" method="post" action="@Url.SubRouteUrl("paste", "Paste.Action", new { action = "Paste" })">
<!form class="form-horizontal" name="editor" method="post" action="@Url.SubRouteUrl("paste", "Paste.Action", new { action = "Paste" })">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-1">
<textarea class="form-control" name="Content" id="content" rows="20"></textarea>
@@ -31,13 +29,14 @@
<label for="syntax" class="col-sm-2 col-sm-offset-1 control-label">Syntax</label>
<div class="col-sm-4">
<select class="form-control" name="Syntax" id="syntax">
<!option value="text">Text only</!option>
@{
@*foreach (Highlighter.Lexer format in Highlighter.Lexers.OrderBy(l => l.Name))
{
<option value="@format.Aliases.FirstOrDefault()">@format.Name</option>
}*@
<!option value="auto-detect">Auto Detect</!option>
<!option value="">Text</!option>
@foreach (var format in HighlightHelper.Languages.GroupBy(l => l.Value).ToList())
{
<!option value="@(format?.FirstOrDefault().Key)">@(format?.Key)</!option>
}

</select>
</div>
</div>
@@ -73,7 +72,7 @@
</div>
</div>
</div>
</form>
</!form>
</div>
</div>
<div class="text-center">
@@ -81,19 +80,4 @@
</div>
</div>

<bundle src="js/paste.min.js" append-version="true"></bundle>

<script>
$("select[name='ExpireUnit']").change(function () {
if ($(this).val() == "never") {
$('#length-div').addClass("hidden");
$('#unit-div').removeClass("col-sm-2");
$('#unit-div').addClass("col-sm-4");
}
else {
$('#length-div').removeClass("hidden");
$('#unit-div').removeClass("col-sm-4");
$('#unit-div').addClass("col-sm-2");
}
});
</script>
<bundle src="js/paste.min.js" append-version="true"></bundle>

+ 34
- 7
Teknik/Areas/Paste/Views/Paste/Simple.cshtml View File

@@ -1,13 +1,31 @@
@model Teknik.Areas.Paste.ViewModels.PasteViewModel

@{
Layout = "";
string syntax = string.Empty;
if (Model.Syntax != "auto-detect")
@{
Layout = null;
string format = Model.Syntax;
string formatName = "Text";
bool useFormat = true;
bool autoDetect = false;

if (string.IsNullOrEmpty(format))
{
useFormat = false;
}
else if (format == "auto-detect")
{
syntax = Model.Syntax;
formatName = "Auto Detect";
autoDetect = true;
}
else if (!HighlightHelper.Languages.ContainsKey(format))
{
useFormat = false;
}
else
{
formatName = HighlightHelper.Languages[format];
}
}

<!DOCTYPE html>
<html>
<head>
@@ -21,10 +39,19 @@

<bundle src="css/paste.min.css" append-version="true"></bundle>
</head>
<body data-twttr-rendered="true">
@Html.Raw(Model.Content)
<body data-twttr-rendered="true">
<script>
var format = '@format';
var useFormat = @(useFormat.ToString().ToLower());
var autoDetect = @(autoDetect.ToString().ToLower());
var highlightWorkerSrc = '@Url.FullURL("~/js/highlight.worker.min.js")';
var highlightSrc = '@Url.FullURL("~/js/highlight.min.js")';
</script>

<pre><code id="code">@Html.Raw(Model.Content)</code></pre>
<bundle src="js/common.min.js" append-version="true"></bundle>
<bundle src="js/paste.view.min.js" append-version="true"></bundle>

<script>
$(document).ready(function () { pageloadDoTimer(); });

+ 1
- 3
Teknik/Middleware/ErrorHandlerMiddleware.cs View File

@@ -57,9 +57,7 @@ namespace Teknik.Middleware
}

// Do nothing if a response body has already been provided or not 404 response
if (httpContext.Response.HasStarted
|| httpContext.Response.ContentLength.HasValue
|| !string.IsNullOrEmpty(httpContext.Response.ContentType))
if (httpContext.Response.HasStarted)
{
return;
}

+ 12
- 22
Teknik/Scripts/Paste/Paste.js View File

@@ -1,26 +1,16 @@
$(document).ready(function () {
$('#content').focus();

linkCreateVault($('#create-vault'));

$('#add-to-vault-menu').find('.add-to-vault').each(function () {
linkAddToVault($(this));
});
});

function linkCreateVault(element) {
element.click(function () {
var pasteUrl = $(this).data('paste-url');
var pasteTitle = $(this).data('paste-title');
window.open(addParamsToUrl(createVaultURL, { items: encodeURIComponent(pasteUrl + ':' + pasteTitle) }), '_blank');
});
}

function linkAddToVault(element) {
element.click(function () {
var addToVaultURL = $(this).data('add-to-vault-url');
var pasteUrl = $(this).data('paste-url');
var pasteTitle = $(this).data('paste-title');
window.open(addParamsToUrl(addToVaultURL, { items: encodeURIComponent(pasteUrl + ':' + pasteTitle) }), '_blank');
$("select[name='ExpireUnit']").change(function () {
if ($(this).val() == "never") {
$('#length-div').addClass("hidden");
$('#unit-div').removeClass("col-sm-2");
$('#unit-div').addClass("col-sm-4");
}
else {
$('#length-div').removeClass("hidden");
$('#unit-div').removeClass("col-sm-4");
$('#unit-div').addClass("col-sm-2");
}
});
}
});

+ 0
- 1
Teknik/Scripts/Paste/Simple.js View File

@@ -1 +0,0 @@
import 'highlight.js';

+ 42
- 0
Teknik/Scripts/Paste/ViewPaste.js View File

@@ -0,0 +1,42 @@
$(document).ready(function () {
linkCreateVault($('#create-vault'));

$('#add-to-vault-menu').find('.add-to-vault').each(function () {
linkAddToVault($(this));
});

if (useFormat) {
var code = document.querySelector('#code');
var worker = new Worker(GenerateBlobURL(highlightWorkerSrc));
var scriptBlob = GenerateBlobURL(highlightSrc);
worker.onmessage = function (event) {
code.innerHTML = event.data.value;
if (autoDetect) {
$('#syntaxLanguage').html('Auto-Detect (' + event.data.language + ')');
}
}
worker.postMessage({
content: code.textContent,
script: scriptBlob,
format: format,
autoDetect: autoDetect
});
}
});

function linkCreateVault(element) {
element.click(function () {
var pasteUrl = $(this).data('paste-url');
var pasteTitle = $(this).data('paste-title');
window.open(addParamsToUrl(createVaultURL, { items: encodeURIComponent(pasteUrl + ':' + pasteTitle) }), '_blank');
});
}

function linkAddToVault(element) {
element.click(function () {
var addToVaultURL = $(this).data('add-to-vault-url');
var pasteUrl = $(this).data('paste-url');
var pasteTitle = $(this).data('paste-title');
window.open(addParamsToUrl(addToVaultURL, { items: encodeURIComponent(pasteUrl + ':' + pasteTitle) }), '_blank');
});
}

+ 11
- 0
Teknik/Scripts/Paste/highlight.worker.js View File

@@ -0,0 +1,11 @@
onmessage = function (event) {
importScripts(event.data.script);
var result;
if (event.data.autoDetect) {
result = self.hljs.highlightAuto(event.data.content);
}
else {
result = self.hljs.highlight(event.data.format, event.data.content);
}
postMessage(result);
}

+ 3
- 0
Teknik/Scripts/lib/highlight.pack.js
File diff suppressed because it is too large
View File


+ 5
- 0
Teknik/Startup.cs View File

@@ -98,6 +98,11 @@ namespace Teknik
options.Providers.Add<GzipCompressionProvider>();
});

services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
});

// Sessions
services.AddResponseCaching();
services.AddMemoryCache();

+ 1
- 1
Teknik/appsettings.json View File

@@ -2,7 +2,7 @@
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-TeknikCore-BE9563D1-0DFB-4141-903C-287B23FF22C7;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"server.urls": "http://localhost:5000;http://localhost:8086",
"server.urls": "http://localhost:5000;https://localhost:5001;http://localhost:8086",
"Logging": {
"LogLevel": {
"Default": "Warning"

+ 18
- 1
Teknik/bundleconfig.json View File

@@ -65,10 +65,21 @@
"./wwwroot/css/app/Home/Home.css"
]
},
{
"outputFileName": "./wwwroot/js/highlight.min.js",
"inputFiles": [
"./wwwroot/js/app/lib/highlight.pack.js"
]
},
{
"outputFileName": "./wwwroot/js/highlight.worker.min.js",
"inputFiles": [
"./wwwroot/js/app/Paste/highlight.worker.js"
]
},
{
"outputFileName": "./wwwroot/js/paste.min.js",
"inputFiles": [
"./wwwroot/lib/highlight/js/highlight.js",
"./wwwroot/js/app/Paste/Paste.js"
]
},
@@ -79,6 +90,12 @@
"./wwwroot/css/app/Paste/Paste.css"
]
},
{
"outputFileName": "./wwwroot/js/paste.view.min.js",
"inputFiles": [
"./wwwroot/js/app/Paste/ViewPaste.js"
]
},
{
"outputFileName": "./wwwroot/js/podcast.min.js",
"inputFiles": [

+ 1
- 2
Teknik/gulpfile.js View File

@@ -34,7 +34,6 @@ var assets = [
{ './node_modules/file-saver/FileSaver.js': 'lib/file-saver/js' },
{ './node_modules/filesize/lib/filesize.js': 'lib/filesize/js' },
{ './node_modules/highcharts/js/highcharts.js': 'lib/highcharts/js' },
{ './node_modules/highlight.js/lib/highlight.js': 'lib/highlight/js' },
{ './node_modules/jquery/dist/jquery.js': 'lib/jquery/js' },
{ './node_modules/block-ui/jquery.BlockUI.js': 'lib/jquery/js' },
{ './node_modules/jquery-validation/dist/jquery.validate.js': 'lib/jquery/js' },
@@ -52,7 +51,7 @@ var assets = [
{ './node_modules/dropzone/dist/dropzone.css': 'lib/dropzone/css' },
{ './node_modules/font-awesome/css/font-awesome.css': 'lib/font-awesome/css' },
{ './node_modules/highcharts/css/highcharts.css': 'lib/highcharts/css' },
{ './node_modules/highlight.js/styles/github-gist.css': 'lib/highlight/css' },
{ './node_modules/highlight.js/styles/*': 'lib/highlight/css' },

// App CSS Files
{ './Content/**/*': 'css/app' },

+ 296
- 0
Utilities/HighlightHelper.cs View File

@@ -0,0 +1,296 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Teknik.Utilities
{
public static class HighlightHelper
{
public static Dictionary<string, string> Languages
{
get
{
return new Dictionary<string, string>()
{
{ "1c", "1C" },
{ "abnf", "ABNF" },
{ "accesslog", "Access logs" },
{ "ada", "Ada" },
{ "armasm", "ARM assembler" },
{ "arm", "ARM assembler" },
{ "avrasm", "AVR assembler" },
{ "actionscript", "ActionScript" },
{ "as", "ActionScript" },
{ "apache", "Apache" },
{ "apacheconf", "Apache" },
{ "applescript", "AppleScript" },
{ "osascript", "AppleScript" },
{ "asciidoc", "AsciiDoc" },
{ "adoc", "AsciiDoc" },
{ "aspectj", "AspectJ" },
{ "autohotkey", "AutoHotkey" },
{ "autoit", "AutoIt" },
{ "awk", "Awk" },
{ "mawk", "Awk" },
{ "nawk", "Awk" },
{ "gawk", "Awk" },
{ "axapta", "Axapta" },
{ "bash", "Bash" },
{ "sh", "Bash" },
{ "zsh", "Bash" },
{ "basic", "Basic" },
{ "bnf", "BNF" },
{ "brainfuck", "Brainfuck" },
{ "bf", "Brainfuck" },
{ "cs", "C#" },
{ "csharp", "C#" },
{ "cpp", "C++" },
{ "c", "C" },
{ "cc", "C++" },
{ "h", "C++" },
{ "c++", "C++" },
{ "h++", "C++" },
{ "hpp", "C++" },
{ "cal", "C/AL" },
{ "cos", "Cache Object Script" },
{ "cls", "Cache Object Script" },
{ "cmake", "CMake" },
{ "cmake.in", "CMake" },
{ "coq", "Coq" },
{ "csp", "CSP" },
{ "css", "CSS" },
{ "capnproto", "Cap’n Proto" },
{ "capnp", "Cap’n Proto" },
{ "clojure", "Clojure" },
{ "clj", "Clojure" },
{ "coffeescript", "CoffeeScript" },
{ "coffee", "CoffeeScript" },
{ "cson", "CoffeeScript" },
{ "iced", "CoffeeScript" },
{ "crmsh", "Crmsh" },
{ "crm", "Crmsh" },
{ "pcmk", "Crmsh" },
{ "crystal", "Crystal" },
{ "cr", "Crystal" },
{ "d", "D" },
{ "dns", "DNS Zone file" },
{ "zone", "DNS Zone file" },
{ "bind", "DNS Zone file" },
{ "dos", "DOS" },
{ "bat", "DOS" },
{ "cmd", "DOS" },
{ "dart", "Dart" },
{ "delphi", "Delphi" },
{ "dpr", "Delphi" },
{ "dfm", "Delphi" },
{ "pas", "Delphi" },
{ "pascal", "Delphi" },
{ "freepascal", "Delphi" },
{ "lazarus", "Delphi" },
{ "lpr", "Delphi" },
{ "lfm", "Delphi" },
{ "diff", "Diff" },
{ "patch", "Diff" },
{ "django", "Django" },
{ "jinja", "Django" },
{ "dockerfile", "Dockerfile" },
{ "docker", "Dockerfile" },
{ "dsconfig", "dsconfig" },
{ "dts", "DTS (Device Tree)" },
{ "dust", "Dust" },
{ "dst", "Dust" },
{ "ebnf", "EBNF" },
{ "elixir", "Elixir" },
{ "elm", "Elm" },
{ "erlang", "Erlang" },
{ "erl", "Erlang" },
{ "excel", "Excel" },
{ "xls", "Excel" },
{ "xlsx", "Excel" },
{ "fsharp", "F#" },
{ "fs", "F#" },
{ "fix", "FIX" },
{ "fortran", "Fortran" },
{ "f90", "Fortran" },
{ "f95", "Fortran" },
{ "gcode", "G-Code" },
{ "nc", "G-Code" },
{ "gams", "Gams" },
{ "gms", "Gams" },
{ "gauss", "GAUSS" },
{ "gss", "GAUSS" },
{ "gherkin", "Gherkin" },
{ "go", "Go" },
{ "golang", "Go" },
{ "golo", "Golo" },
{ "gololang", "Golo" },
{ "gradle", "Gradle" },
{ "groovy", "Groovy" },
{ "xml", "XML" },
{ "html", "HTML" },
{ "xhtml", "XHTML" },
{ "rss", "RSS" },
{ "atom", "Atom" },
{ "xjb", "HTML, XML" },
{ "xsd", "HTML, XML" },
{ "xsl", "HTML, XML" },
{ "plist", "HTML, XML" },
{ "http", "HTTP" },
{ "https", "HTTP" },
{ "haml", "Haml" },
{ "handlebars", "Handlebars" },
{ "hbs", "Handlebars" },
{ "html.hbs", "Handlebars" },
{ "html.handlebars", "Handlebars" },
{ "haskell", "Haskell" },
{ "hs", "Haskell" },
{ "haxe", "Haxe" },
{ "hx", "Haxe" },
{ "hy", "Hy" },
{ "hylang", "Hy" },
{ "ini", "Ini" },
{ "inform7", "Inform7" },
{ "i7", "Inform7" },
{ "irpf90", "IRPF90" },
{ "json", "JSON" },
{ "java", "Java" },
{ "jsp", "Java" },
{ "javascript", "JavaScript" },
{ "js", "JavaScript" },
{ "jsx", "JavaScript" },
{ "leaf", "Leaf" },
{ "lasso", "Lasso" },
{ "lassoscript", "Lasso" },
{ "less", "Less" },
{ "ldif", "LDIF" },
{ "lisp", "Lisp" },
{ "livecodeserver", "LiveCode Server" },
{ "livescript", "LiveScript" },
{ "ls", "LiveScript" },
{ "lua", "Lua" },
{ "makefile", "Makefile" },
{ "mk", "Makefile" },
{ "mak", "Makefile" },
{ "markdown", "Markdown" },
{ "md", "Markdown" },
{ "mkdown", "Markdown" },
{ "mkd", "Markdown" },
{ "mathematica", "Mathematica" },
{ "mma", "Mathematica" },
{ "matlab", "Matlab" },
{ "maxima", "Maxima" },
{ "mel", "Maya Embedded Language" },
{ "mercury", "Mercury" },
{ "mizar", "Mizar" },
{ "mojolicious", "Mojolicious" },
{ "monkey", "Monkey" },
{ "moonscript", "Moonscript" },
{ "moon", "Moonscript" },
{ "n1ql", "N1QL" },
{ "nsis", "NSIS" },
{ "nginx", "Nginx" },
{ "nginxconf", "Nginx" },
{ "nimrod", "Nimrod" },
{ "nim", "Nimrod" },
{ "nix", "Nix" },
{ "ocaml", "OCaml" },
{ "ml", "OCaml" },
{ "objectivec", "Objective C" },
{ "mm", "Objective C" },
{ "objc", "Objective C" },
{ "obj-c", "Objective C" },
{ "glsl", "OpenGL Shading Language" },
{ "openscad", "OpenSCAD" },
{ "scad", "OpenSCAD" },
{ "ruleslanguage", "Oracle Rules Language" },
{ "oxygene", "Oxygene" },
{ "pf", "PF" },
{ "pf.conf", "PF" },
{ "php", "PHP" },
{ "php3", "PHP" },
{ "php4", "PHP" },
{ "php5", "PHP" },
{ "php6", "PHP" },
{ "parser3", "Parser3" },
{ "perl", "Perl" },
{ "pl", "Perl" },
{ "pm", "Perl" },
{ "pony", "Pony" },
{ "powershell", "PowerShell" },
{ "ps", "PowerShell" },
{ "processing", "Processing" },
{ "prolog", "Prolog" },
{ "protobuf", "Protocol Buffers" },
{ "puppet", "Puppet" },
{ "pp", "Puppet" },
{ "python", "Python" },
{ "py", "Python" },
{ "gyp", "Python" },
{ "profile", "Python profiler results" },
{ "k", "Q" },
{ "kdb", "Q" },
{ "qml", "QML" },
{ "r", "R" },
{ "rib", "RenderMan RIB" },
{ "rsl", "RenderMan RSL" },
{ "graph", "Roboconf" },
{ "instances", "Roboconf" },
{ "ruby", "Ruby" },
{ "rb", "Ruby" },
{ "gemspec", "Ruby" },
{ "podspec", "Ruby" },
{ "thor", "Ruby" },
{ "irb", "Ruby" },
{ "rust", "Rust" },
{ "rs", "Rust" },
{ "scss", "SCSS" },
{ "sql", "SQL" },
{ "p21", "STEP Part 21" },
{ "step", "STEP Part 21" },
{ "stp", "STEP Part 21" },
{ "scala", "Scala" },
{ "scheme", "Scheme" },
{ "scilab", "Scilab" },
{ "sci", "Scilab" },
{ "shell", "Shell" },
{ "console", "Shell" },
{ "smali", "Smali" },
{ "smalltalk", "Smalltalk" },
{ "st", "Smalltalk" },
{ "stan", "Stan" },
{ "stata", "Stata" },
{ "stylus", "Stylus" },
{ "styl", "Stylus" },
{ "subunit", "SubUnit" },
{ "swift", "Swift" },
{ "tap", "Test Anything Protocol" },
{ "tcl", "Tcl" },
{ "tk", "Tcl" },
{ "tex", "TeX" },
{ "thrift", "Thrift" },
{ "tp", "TP" },
{ "twig", "Twig" },
{ "craftcms", "Twig" },
{ "typescript", "TypeScript" },
{ "ts", "TypeScript" },
{ "vbnet", "VB.Net" },
{ "vb", "VB.Net" },
{ "vbscript", "VBScript" },
{ "vbs", "VBScript" },
{ "vhdl", "VHDL" },
{ "vala", "Vala" },
{ "verilog", "Verilog" },
{ "v", "Verilog" },
{ "vim", "Vim Script" },
{ "x86asm", "x86 Assembly" },
{ "xl", "XL" },
{ "tao", "XL" },
{ "xpath", "XQuery" },
{ "xq", "XQuery" },
{ "zephir", "Zephir" },
{ "zep", "Zephir" }
};
}
}
}
}

Loading…
Cancel
Save