cmd/web: syntax highlighting with prism.js

Signed-off-by: Xe Iaso <xe@tailscale.com>
This commit is contained in:
Xe Iaso 2023-02-22 14:27:53 +00:00
parent 4e3a9a3757
commit a6de30a985
10 changed files with 334 additions and 5 deletions

View file

@ -19,6 +19,7 @@ import (
"text/template"
"time"
"github.com/go-enry/go-enry/v2"
"github.com/google/uuid"
"github.com/tailscale/sqlite"
"tailscale.com/client/tailscale"
@ -465,6 +466,13 @@ WHERE p.id = ?1`
}
}
lang, _ := enry.GetLanguageByContent(fname, []byte(data))
var cssClass string
if lang != "" {
cssClass = fmt.Sprintf("lang-%s", lang)
}
err = s.tmpls.ExecuteTemplate(w, "showpaste.tmpl", struct {
UserInfo *tailcfg.UserProfile
Title string
@ -475,6 +483,7 @@ WHERE p.id = ?1`
UserID int64
ID string
Data string
CSSClass string
}{
UserInfo: up,
Title: fname,
@ -485,6 +494,7 @@ WHERE p.id = ?1`
UserID: int64(up.ID),
ID: id,
Data: data,
CSSClass: cssClass,
})
if err != nil {
log.Printf("%s: %v", r.RemoteAddr, err)

View file

@ -0,0 +1,3 @@
/* PrismJS 1.29.0
https://prismjs.com/download.html#themes=prism&languages=markup+css+clike+javascript+abap+abnf+actionscript+ada+agda+al+antlr4+apacheconf+apex+apl+applescript+aql+arduino+arff+armasm+arturo+asciidoc+aspnet+asm6502+asmatmel+autohotkey+autoit+avisynth+avro-idl+awk+bash+basic+batch+bbcode+bbj+bicep+birb+bison+bnf+bqn+brainfuck+brightscript+bro+bsl+c+csharp+cpp+cfscript+chaiscript+cil+cilkc+cilkcpp+clojure+cmake+cobol+coffeescript+concurnas+csp+cooklang+coq+crystal+css-extras+csv+cue+cypher+d+dart+dataweave+dax+dhall+diff+django+dns-zone-file+docker+dot+ebnf+editorconfig+eiffel+ejs+elixir+elm+etlua+erb+erlang+excel-formula+fsharp+factor+false+firestore-security-rules+flow+fortran+ftl+gml+gap+gcode+gdscript+gedcom+gettext+gherkin+git+glsl+gn+linker-script+go+go-module+gradle+graphql+groovy+haml+handlebars+haskell+haxe+hcl+hlsl+hoon+http+hpkp+hsts+ichigojam+icon+icu-message-format+idris+ignore+inform7+ini+io+j+java+javadoc+javadoclike+javastacktrace+jexl+jolie+jq+jsdoc+js-extras+json+json5+jsonp+jsstacktrace+js-templates+julia+keepalived+keyman+kotlin+kumir+kusto+latex+latte+less+lilypond+liquid+lisp+livescript+llvm+log+lolcode+lua+magma+makefile+markdown+markup-templating+mata+matlab+maxscript+mel+mermaid+metafont+mizar+mongodb+monkey+moonscript+n1ql+n4js+nand2tetris-hdl+naniscript+nasm+neon+nevod+nginx+nim+nix+nsis+objectivec+ocaml+odin+opencl+openqasm+oz+parigp+parser+pascal+pascaligo+psl+pcaxis+peoplecode+perl+php+phpdoc+php-extras+plant-uml+plsql+powerquery+powershell+processing+prolog+promql+properties+protobuf+pug+puppet+pure+purebasic+purescript+python+qsharp+q+qml+qore+r+racket+cshtml+jsx+tsx+reason+regex+rego+renpy+rescript+rest+rip+roboconf+robotframework+ruby+rust+sas+sass+scss+scala+scheme+shell-session+smali+smalltalk+smarty+sml+solidity+solution-file+soy+sparql+splunk-spl+sqf+sql+squirrel+stan+stata+iecst+stylus+supercollider+swift+systemd+t4-templating+t4-cs+t4-vb+tap+tcl+tt2+textile+toml+tremor+turtle+twig+typescript+typoscript+unrealscript+uorazor+uri+v+vala+vbnet+velocity+verilog+vhdl+vim+visual-basic+warpscript+wasm+web-idl+wgsl+wiki+wolfram+wren+xeora+xml-doc+xojo+xquery+yaml+yang+zig */
code[class*=language-],pre[class*=language-]{color:#000;background:0 0;text-shadow:0 1px #fff;font-family:Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;font-size:1em;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}code[class*=language-] ::-moz-selection,code[class*=language-]::-moz-selection,pre[class*=language-] ::-moz-selection,pre[class*=language-]::-moz-selection{text-shadow:none;background:#b3d4fc}code[class*=language-] ::selection,code[class*=language-]::selection,pre[class*=language-] ::selection,pre[class*=language-]::selection{text-shadow:none;background:#b3d4fc}@media print{code[class*=language-],pre[class*=language-]{text-shadow:none}}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code[class*=language-],pre[class*=language-]{background:#f5f2f0}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#708090}.token.punctuation{color:#999}.token.namespace{opacity:.7}.token.boolean,.token.constant,.token.deleted,.token.number,.token.property,.token.symbol,.token.tag{color:#905}.token.attr-name,.token.builtin,.token.char,.token.inserted,.token.selector,.token.string{color:#690}.language-css .token.string,.style .token.string,.token.entity,.token.operator,.token.url{color:#9a6e3a;background:hsla(0,0%,100%,.5)}.token.atrule,.token.attr-value,.token.keyword{color:#07a}.token.class-name,.token.function{color:#dd4a68}.token.important,.token.regex,.token.variable{color:#e90}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help}

View file

@ -5,7 +5,7 @@
--font-family: "OpenDyslexic", sans-serif;
--mono-font-family: "OpenDyslexicMono", monospace;
--width: 45rem;
--width: 55rem;
--padding: 0;
--selection: hsla(var(--accent-color), 80%, 30%, 100%);

300
cmd/web/static/js/prism.js Normal file

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,8 @@
<html>
<head>
<title>{{.Title}}</title>
<link rel="stylesheet" href="/static/css/xess.css">
<link rel="stylesheet" href="/static/css/xess.css" />
<link rel="stylesheet" href="/static/css/prism.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body id="top">

View file

@ -1,7 +1,5 @@
{{template "header" .}}
<div class="right"><img style="width:32px;height:32px" src="{{.PasterProfilePicURL}}" /></div>
<p>There are no pastes in the list. Try <a href="/">creating some</a>!</p>
{{template "footer" .}}

View file

@ -1,11 +1,16 @@
{{template "header" .}}
<script src="/static/js/prism.js"></script>
<div class="right">{{.PasterDisplayName}} <img style="width:32px;height:32px" src="{{.PasterProfilePicURL}}" /></div>
<p>Created at {{.CreatedAt}}</p>
<pre><code>{{.Data}}
<div style="display:inline-block;">
<pre><code class="{{.CSSClass}}">{{.Data}}
</code></pre>
</div>
<a href="/paste/{{.ID}}">Permalink</a> - <a href="/paste/{{.ID}}/dl">Download</a> - <a href="/paste/{{.ID}}/raw">Raw</a>{{if eq .UserID .PasterUserID}} - <a href="/api/delete/{{.ID}}">Delete</a>{{end}}
{{template "footer" .}}

2
go.mod
View file

@ -3,6 +3,7 @@ module github.com/tailscale-dev/tclip
go 1.19
require (
github.com/go-enry/go-enry/v2 v2.8.3
github.com/google/uuid v1.3.0
github.com/tailscale/sqlite v0.0.0-20221025150348-0716cf4a392b
tailscale.com v1.34.0
@ -27,6 +28,7 @@ require (
github.com/aws/smithy-go v1.9.0 // indirect
github.com/coreos/go-iptables v0.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/go-enry/go-oniguruma v1.2.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/godbus/dbus/v5 v5.0.6 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect

4
go.sum
View file

@ -51,6 +51,10 @@ github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/github/fakeca v0.1.0 h1:Km/MVOFvclqxPM9dZBC4+QE564nU4gz4iZ0D9pMw28I=
github.com/github/fakeca v0.1.0/go.mod h1:+bormgoGMMuamOscx7N91aOuUST7wdaJ2rNjeohylyo=
github.com/go-enry/go-enry/v2 v2.8.3 h1:BwvNrN58JqBJhyyVdZSl5QD3xoxEEGYUrRyPh31FGhw=
github.com/go-enry/go-enry/v2 v2.8.3/go.mod h1:GVzIiAytiS5uT/QiuakK7TF1u4xDab87Y8V5EJRpsIQ=
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
github.com/go-enry/go-oniguruma v1.2.1/go.mod h1:bWDhYP+S6xZQgiRL7wlTScFYBe023B6ilRZbCAD5Hf4=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=

View file

@ -55,6 +55,12 @@ schema = 3
[mod."github.com/fxamacker/cbor/v2"]
version = "v2.4.0"
hash = "sha256-LwOrfDN5fx/R2lgaD90RIu2NxcJDpyqYEURLc9Kgxgw="
[mod."github.com/go-enry/go-enry/v2"]
version = "v2.8.3"
hash = "sha256-xVkCSN3Bj1PgLy/Zb5XYYBnOUFKgKsLfAARlPDHe+kA="
[mod."github.com/go-enry/go-oniguruma"]
version = "v1.2.1"
hash = "sha256-DoCNyX75CuCgFnfSZs63VB4+HAIMDBgwcQglXXHRj/I="
[mod."github.com/go-ole/go-ole"]
version = "v1.2.6"
hash = "sha256-+oxitLeJxYF19Z6g+6CgmCHJ1Y5D8raMi2Cb3M6nXCs="