Add line end support

pull/8/head
Leonid Maslakov 1 year ago
parent 16375c1b8b
commit d2315d4624

@ -5,6 +5,7 @@ go 1.11
replace git.lcomrade.su/root/lenpaste/internal => ./internal
require (
git.lcomrade.su/root/lineend v1.0.0
github.com/alecthomas/chroma v0.10.0
github.com/mattn/go-sqlite3 v1.14.13
)

@ -1,3 +1,5 @@
git.lcomrade.su/root/lineend v1.0.0 h1:qcxrR4DS18Erx+pG/EspoDhEDai+mjgSYefwSK2dq5g=
git.lcomrade.su/root/lineend v1.0.0/go.mod h1:D0q3jMx0I1PFZjAFwkmUNW8D5tIw8rZJoeUAxhYD7Ec=
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
github.com/alecthomas/chroma/v2 v2.0.0-alpha4 h1:6s0y/julsg565meUfJd/aDv5nR4srI3Z3RgyId8w3Ro=

@ -20,8 +20,10 @@ package netshare
import (
"git.lcomrade.su/root/lenpaste/internal/storage"
"git.lcomrade.su/root/lineend"
"net/url"
"strconv"
"strings"
"time"
)
@ -35,11 +37,30 @@ func PasteAddFromForm(dbInfo storage.DB, lexerNames []string, form url.Values) (
OneUse: false,
}
// Remove new line from title
paste.Title = strings.Replace(paste.Title, "\n", "", -1)
paste.Title = strings.Replace(paste.Title, "\r", "", -1)
// Check paste body
if paste.Body == "" {
return paste, ErrBadRequest
}
// Change paste body lines end
switch form.Get("lineEnd") {
case "", "LF", "lf":
paste.Body = lineend.UnknownToUnix(paste.Body)
case "CRLF", "crlf":
paste.Body = lineend.UnknownToDos(paste.Body)
case "CR", "cr":
paste.Body = lineend.UnknownToOldMac(paste.Body)
default:
return paste, ErrBadRequest
}
// Check syntax
syntaxOk := false
for _, name := range lexerNames {

@ -28,8 +28,8 @@ import (
type embTmpl struct {
ID string
CreateTimeStr string
DeleteTime int64
OneUse bool
DeleteTime int64
OneUse bool
Title string
Body template.HTML
@ -67,8 +67,8 @@ func (data Data) EmbeddedHand(rw http.ResponseWriter, req *http.Request) {
tmplData := embTmpl{
ID: paste.ID,
CreateTimeStr: createTime.Format("1 Jan, 2006"),
DeleteTime: paste.DeleteTime,
OneUse: paste.OneUse,
DeleteTime: paste.DeleteTime,
OneUse: paste.OneUse,
Title: paste.Title,
Body: template.HTML(bodyHighlight),

@ -25,9 +25,9 @@ import (
)
type embHelpTmpl struct {
ID string
ID string
DeleteTime int64
OneUse bool
OneUse bool
Protocol string
Host string
@ -56,11 +56,11 @@ func (data Data) EmbeddedHelpHand(rw http.ResponseWriter, req *http.Request) {
// Show paste
tmplData := embHelpTmpl{
ID: paste.ID,
ID: paste.ID,
DeleteTime: paste.DeleteTime,
OneUse: paste.OneUse,
Protocol: netshare.GetProtocol(req.Header),
Host: netshare.GetHost(req),
OneUse: paste.OneUse,
Protocol: netshare.GetProtocol(req.Header),
Host: netshare.GetHost(req),
}
err = data.EmbeddedHelpPage.Execute(rw, tmplData)

@ -20,6 +20,7 @@ package web
import (
"git.lcomrade.su/root/lenpaste/internal/storage"
"git.lcomrade.su/root/lineend"
"html/template"
"net/http"
"time"
@ -34,6 +35,7 @@ type pasteTmpl struct {
DeleteTime int64
OneUse bool
LineEnd string
CreateTimeStr string
DeleteTimeStr string
}
@ -98,6 +100,16 @@ func (data Data) getPaste(rw http.ResponseWriter, req *http.Request) {
DeleteTimeStr: deleteTime.Format("15:04 02.01.2006 UTC"),
}
// Get body line end
switch lineend.GetLineEnd(paste.Body) {
case "\r\n":
tmplData.LineEnd = "CRLF"
case "\r":
tmplData.LineEnd = "CR"
default:
tmplData.LineEnd = "LF"
}
// Show paste
err = data.PastePage.Execute(rw, tmplData)
if err != nil {

@ -31,6 +31,16 @@
<td></td>
<td>Paste text.</td>
</tr>
<tr>
<td><code>lineEnd</code></td>
<td></td>
<td><code>LF</code></td>
<td>
The end of a line in the paste body.
The text will be automatically recoded to match the selected end of line.
Can accept <code>LF</code>, <code>CRLF</code> or <code>CR</code>.
</td>
</tr>
<tr>
<td><code>syntax</code></td>
<td></td>
@ -54,7 +64,7 @@
<pre><code>{
"id": "XcmX9ON1",
"title": "Paste title.",
"body": "Line 1.\r\nLine 2.\r\nLine 3.\r\n\r\nLine 5.",
"body": "Line 1.\nLine 2.\nLine 3.\n\nLine 5.",
"createTime": 1653387358,
"deleteTime": 0,
"oneUse": false

@ -2,19 +2,28 @@
{{define "article"}}
<h3>Create paste</h3>
<form action="/" method="post">
<div><input
class="stretch-width" name="title"
autocomplete="off" autocorrect="off" spellcheck="true"
placeholder="Enter title..." tabindex=1 autofocus
<div class="text-bar">
<div><input
class="stretch-width" name="title"
autocomplete="off" autocorrect="off" spellcheck="true"
placeholder="Enter title..." tabindex=1 autofocus
></div>
<div class="text-bar-right">
<select name="lineEnd" tabindex=2 size=1>
<option value="LF" selected="true">UNIX</option>
<option value="CRLF">Windows/DOS</option>
<option value="CR">Macintosh</option>
</select>
</div>
</div>
<div><textarea
name="body" placeholder="Enter text..."
autocomplete="off" autocorrect="off" spellcheck="true"
rows=20 wrap="off" tabindex=2 required
rows=20 wrap="off" tabindex=3 required
></textarea></div>
<div>
<label for="syntax">Syntax:</label>
<select name="syntax" tabindex=3 size=1>
<select name="syntax" tabindex=4 size=1>
{{range .Lexers}}
<option value="{{.}}"{{if eq . "plaintext"}} selected="true"{{end}}>{{.}}</option>
{{end}}
@ -22,11 +31,11 @@
</div>
<div>
<label class="checkbox">
<input type="checkbox" name="oneUse" value="true" tabindex=4></input>Burn after reading</label>
<input type="checkbox" name="oneUse" value="true" tabindex=5></input>Burn after reading</label>
</div>
<div>
<label for="expiration">Expiration:</label>
<select name="expiration" tabindex=5 size=1>
<select name="expiration" tabindex=6 size=1>
<option value="0" selected="true">Never</option>
<option value="600">10 minutes</option>
<option value="1800">30 minutes</option>
@ -43,6 +52,6 @@
<option value="31557600">1 Year</option>
</select>
</div>
<div><button class="button-green" type="submit" tabindex=6>Create paste</button></div>
<div><button class="button-green" type="submit" tabindex=7>Create paste</button></div>
</form>
{{end}}

@ -4,7 +4,7 @@
{{end}}
<div class="text-bar">
<div>{{.Syntax}}</div>
<div>{{.Syntax}}, {{.LineEnd}}</div>
{{if not .OneUse}}
<div class="text-bar-right">

@ -88,10 +88,14 @@ article {
.text-bar {
display: flex;
justify-content: space-between;
justify-content: start;
font-family: monospace;
}
.text-bar div {
width: 50%;
}
.text-bar-right {
align-self: flex-end;
text-align: right;

Loading…
Cancel
Save