32 Commits
Author SHA1 Message Date
Matt Baer 28cf6b3866 Fix TestNewImageAttachment tests 2021-09-13 15:32:27 -04:00
Matt Baer 8b45bd4fcc Support creating Document attachments
In particular, this will support audio attachments.

Ref T872
2021-09-13 15:32:10 -04:00
Matt Baer fd1559928a Copy post parsing funcs to posts pkg 2021-04-13 23:49:59 -04:00
Matt Baer 95a3a717ed Move remaining writeas/nerds/store funcs to web-core
Finishes the work started in #8.
2021-03-30 12:44:22 -04:00
Matt Baer 8dbd86535d Don't return pointer from NewImageAttachment() 2021-03-24 15:47:23 -04:00
Matt Baer f534cc9f9b Fix TagMention type 2021-03-24 15:46:13 -04:00
Matt Baer a77cafb3ae Add NewImageAttachment func 2021-03-24 15:45:58 -04:00
Matt Baer 48cbbf652a Support attachments on activitystreams.Object 2021-03-24 15:33:34 -04:00
Matt BaerandGitHub e05f572eb2 Merge pull request #11 from colin-axner/colin/memo-reset
Add (forceful) Reset function to memo
2021-03-06 16:51:49 -05:00
Colin Axner 7ddb288ae3 add godoc 2020-12-06 12:06:51 +01:00
Colin Axner cee889bd58 Add Reset func to memo
Add a new function Reset to memo to forcibly reset the cache
2020-11-23 15:36:27 +01:00
Matt BaerandGitHub aacd7d3d0c Merge pull request #4 from laosb/patch-1
Improve Chinese translation.
2020-09-24 10:58:41 -04:00
Matt BaerandGitHub 3134869cce Merge pull request #8 from eddsalkield/nerdsnipe
Remove dependency on github.com/writeas/nerds
2020-09-24 08:45:19 -04:00
Matt BaerandGitHub 083de67f45 Merge pull request #6 from eddsalkield/uuid
Migrate to new uuid library
2020-09-24 08:25:13 -04:00
Edd Salkield 339af195c2 Remove dependency on github.com/writeas/nerds 2020-09-22 22:14:10 +00:00
Edd Salkield 83e2689111 Migrate to new uuid library 2020-09-22 22:06:36 +00:00
Matt Baer 68a680d1b0 Add silobridge package 2020-08-13 12:17:34 -04:00
Matt Baer f54ee176fc Remove commentsEnabled AP extension
This isn't used.
2019-11-11 21:56:08 +09:00
Matt Baer 45b1985b97 Change Write.as -> WriteFreely in README 2019-11-11 21:48:19 +09:00
Wexpo LyuandGitHub 35ecbe8f09 Update Chinese translation.
- Make sentences concise.
- Implement spaces between Chinese and Western characters.
2019-10-26 17:42:19 +08:00
Matt Baer 46304124ec Document posts.ExtractTitle func 2019-05-30 10:12:34 -04:00
Matt Baer 1f29da565f Include calling filename and line # in error logs
...instead of the filename and line number of the Printf call in
log.Error()
2019-05-23 06:59:22 -04:00
Matt BaerandGitHub 8dbb4ebdc5 Merge pull request #3 from gytisrepecka/master
Added phrases_lt.go
2019-05-08 09:51:00 -04:00
Gytis Repečka c5dba02ee6 Added Lithuanian localization. 2019-05-08 08:18:35 +03:00
Matt Baer 7b4bf5c1b2 Include extensions when building Create & Update activities 2019-04-04 16:57:46 -04:00
Matt Baer 78107a0269 Merge branch 'master' of github.com:writeas/web-core 2019-04-04 16:36:11 -04:00
Matt Baer 528e59e922 Update README to explain use in WriteFreely 2019-04-04 16:35:50 -04:00
Matt Baer ad1a4272b0 Support new commentsEnabled property
This adds a CommentsEnabled field to the activitystreams.Object struct,
as well as the Extensions needed in the Context field in order to
describe the field.

This is a field previously supported by PeerTube, and just recently
added in PixelFed.
2019-04-04 16:29:00 -04:00
Matt BaerandGitHub 9884cdaa8a Merge pull request #2 from SamWhited/support_modules
Support Go Modules
2019-02-01 17:30:18 +01:00
Sam Whited cee61547fa Support Go Modules
Fixes #1
2019-02-01 10:28:46 -06:00
Matt Baer ce8cb5ee19 Use #writefreely badge, not Slack 2019-01-10 21:42:25 -05:00
Matt Baer 5631982c2b Support creating word-ish password
This adds the NewWordish() func
2019-01-10 20:46:03 -05:00
19 changed files with 524 additions and 27 deletions
+4 -4
View File
@@ -1,8 +1,8 @@
Write.as web core
=================
WriteFreely web core
====================
[![GoDoc](https://godoc.org/github.com/writeas/web-core?status.svg)](https://godoc.org/github.com/writeas/web-core)
[![Build Status](https://travis-ci.org/writeas/web-core.svg)](https://travis-ci.org/writeas/web-core)
[![Public Slack discussion](http://slack.write.as/badge.svg)](http://slack.write.as/)
[![#writefreely on freenode](https://img.shields.io/badge/freenode-%23writefreely-blue.svg)](http://webchat.freenode.net/?channels=writefreely)
[![Discuss on our forum](https://img.shields.io/discourse/https/discuss.write.as/users.svg?label=forum)](https://discuss.write.as/c/development)
web-core holds components of the [Write.as](https://write.as) web application.
web-core holds components of the [WriteFreely](https://writefreely.org) web application, and is shared with other [Write.as](https://write.as) products, like [Snap.as](https://snap.as).
+8
View File
@@ -11,6 +11,8 @@ const (
toPublic = "https://www.w3.org/ns/activitystreams#Public"
)
var Extensions = map[string]string{}
// Activity describes actions that have either already occurred, are in the
// process of occurring, or may occur in the future.
type Activity struct {
@@ -38,6 +40,7 @@ func NewCreateActivity(o *Object) *Activity {
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
Extensions,
},
ID: o.ID,
Type: "Create",
@@ -56,6 +59,7 @@ func NewUpdateActivity(o *Object) *Activity {
BaseObject: BaseObject{
Context: []interface{}{
Namespace,
Extensions,
},
ID: o.ID,
Type: "Update",
@@ -113,6 +117,10 @@ type Object struct {
Content string `json:"content"`
ContentMap map[string]string `json:"contentMap,omitempty"`
Tag []Tag `json:"tag"`
Attachment []Attachment `json:"attachment,omitempty"`
// Extensions
// NOTE: add extensions here
}
// NewNoteObject creates a basic Note object that includes the public
+47
View File
@@ -0,0 +1,47 @@
package activitystreams
import (
"mime"
"strings"
)
type Attachment struct {
Type AttachmentType `json:"type"`
URL string `json:"url"`
MediaType string `json:"mediaType"`
Name string `json:"name"`
}
type AttachmentType string
const (
AttachImage AttachmentType = "Image"
AttachDocument AttachmentType = "Document"
)
// NewImageAttachment creates a new Attachment from the given URL, setting the
// correct type and automatically detecting the MediaType based on the file
// extension.
func NewImageAttachment(url string) Attachment {
return newAttachment(url, AttachImage)
}
// NewDocumentAttachment creates a new Attachment from the given URL, setting the
// correct type and automatically detecting the MediaType based on the file
// extension.
func NewDocumentAttachment(url string) Attachment {
return newAttachment(url, AttachDocument)
}
func newAttachment(url string, attachType AttachmentType) Attachment {
var fileType string
extIdx := strings.LastIndexByte(url, '.')
if extIdx > -1 {
fileType = mime.TypeByExtension(url[extIdx:])
}
return Attachment{
Type: attachType,
URL: url,
MediaType: fileType,
}
}
+64
View File
@@ -0,0 +1,64 @@
package activitystreams
import (
"reflect"
"testing"
)
func TestNewImageAttachment(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want Attachment
}{
{name: "good svg", args: args{"https://writefreely.org/img/writefreely.svg"}, want: Attachment{
Type: "Image",
URL: "https://writefreely.org/img/writefreely.svg",
MediaType: "image/svg+xml",
}},
{name: "good png", args: args{"https://i.snap.as/12345678.png"}, want: Attachment{
Type: "Image",
URL: "https://i.snap.as/12345678.png",
MediaType: "image/png",
}},
{name: "no extension", args: args{"https://i.snap.as/12345678"}, want: Attachment{
Type: "Image",
URL: "https://i.snap.as/12345678",
MediaType: "",
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewImageAttachment(tt.args.url); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewImageAttachment() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewDocumentAttachment(t *testing.T) {
type args struct {
url string
}
tests := []struct {
name string
args args
want Attachment
}{
{name: "mp3", args: args{"https://listen.as/matt/abc.mp3"}, want: Attachment{
Type: "Document",
URL: "https://listen.as/matt/abc.mp3",
MediaType: "audio/mpeg",
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewDocumentAttachment(tt.args.url); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewDocumentAttachment() = %+v, want %+v", got, tt.want)
}
})
}
}
+1 -1
View File
@@ -10,5 +10,5 @@ type TagType string
const (
TagHashtag TagType = "Hashtag"
TagMention = "Mention"
TagMention TagType = "Mention"
)
+3 -3
View File
@@ -1,7 +1,7 @@
package auth
import (
uuid "github.com/nu7hatch/gouuid"
uuid "github.com/gofrs/uuid"
"github.com/writeas/web-core/log"
"strings"
)
@@ -16,7 +16,7 @@ func GetToken(header string) []byte {
token = f[1]
}
}
t, err := uuid.ParseHex(token)
t, err := uuid.FromString(token)
if err != nil {
log.Error("Couldn't parseHex on '%s': %v", accessToken, err)
} else {
@@ -31,7 +31,7 @@ func GetHeaderToken(header string) []byte {
if len(header) > 0 {
f := strings.Fields(header)
if len(f) == 2 && f[0] == "Token" {
t, err := uuid.ParseHex(f[1])
t, err := uuid.FromString(f[1])
if err != nil {
log.Error("Couldn't parseHex on '%s': %v", accessToken, err)
} else {
+17
View File
@@ -0,0 +1,17 @@
module github.com/writeas/web-core
go 1.10
require (
github.com/gofrs/uuid v3.3.0+incompatible
github.com/kylemcc/twitter-text-go v0.0.0-20180726194232-7f582f6736ec
github.com/microcosm-cc/bluemonday v1.0.2
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/writeas/go-strip-markdown v2.0.1+incompatible
github.com/writeas/impart v1.1.1
github.com/writeas/openssl-go v1.0.0
github.com/writeas/saturday v1.7.1
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
)
+35
View File
@@ -0,0 +1,35 @@
github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84=
github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylemcc/twitter-text-go v0.0.0-20180726194232-7f582f6736ec h1:ZXWuspqypleMuJy4bzYEqlMhJnGAYpLrWe5p7W3CdvI=
github.com/kylemcc/twitter-text-go v0.0.0-20180726194232-7f582f6736ec/go.mod h1:voECJzdraJmolzPBgL9Z7ANwXf4oMXaTCsIkdiPpR/g=
github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/writeas/go-strip-markdown v2.0.1+incompatible h1:IIqxTM5Jr7RzhigcL6FkrCNfXkvbR+Nbu1ls48pXYcw=
github.com/writeas/go-strip-markdown v2.0.1+incompatible/go.mod h1:Rsyu10ZhbEK9pXdk8V6MVnZmTzRG0alMNLMwa0J01fE=
github.com/writeas/impart v1.1.1 h1:RyA9+CqbdbDuz53k+nXCWUY+NlEkdyw6+nWanxSBl5o=
github.com/writeas/impart v1.1.1/go.mod h1:g0MpxdnTOHHrl+Ca/2oMXUHJ0PcRAEWtkCzYCJUXC9Y=
github.com/writeas/openssl-go v1.0.0 h1:YXM1tDXeYOlTyJjoMlYLQH1xOloUimSR1WMF8kjFc5o=
github.com/writeas/openssl-go v1.0.0/go.mod h1:WsKeK5jYl0B5y8ggOmtVjbmb+3rEGqSD25TppjJnETA=
github.com/writeas/saturday v1.7.1 h1:lYo1EH6CYyrFObQoA9RNWHVlpZA5iYL5Opxo7PYAnZE=
github.com/writeas/saturday v1.7.1/go.mod h1:ETE1EK6ogxptJpAgUbcJD0prAtX48bSloie80+tvnzQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 h1:nVJ3guKA9qdkEQ3TUdXI9QSINo2CUPM/cySEvw2w8I0=
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 h1:POO/ycCATvegFmVuPpQzZFJ+pGZeX22Ufu6fibxDVjU=
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
+28 -2
View File
@@ -1,12 +1,38 @@
package id
import (
"crypto/rand"
"fmt"
"github.com/writeas/nerds/store"
)
// GenerateRandomString creates a random string of characters of the given
// length from the given dictionary of possible characters.
//
// This example generates a hexadecimal string 6 characters long:
// GenerateRandomString("0123456789abcdef", 6)
func GenerateRandomString(dictionary string, l int) string {
var bytes = make([]byte, l)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}
// GenSafeUniqueSlug generatees a reasonably unique random slug from the given
// original slug. It's "safe" because it uses 0-9 b-z excluding vowels.
func GenSafeUniqueSlug(slug string) string {
return fmt.Sprintf("%s-%s", slug, store.GenerateRandomString("0123456789bcdfghjklmnpqrstvwxyz", 4))
return fmt.Sprintf("%s-%s", slug, GenerateRandomString("0123456789bcdfghjklmnpqrstvwxyz", 4))
}
// Generate62RandomString creates a random string with the given length
// consisting of characters in [A-Za-z0-9].
func Generate62RandomString(l int) string {
return GenerateRandomString("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", l)
}
// GenerateFriendlyRandomString creates a random string of characters with the
// given length consisting of characters in [a-z0-9].
func GenerateFriendlyRandomString(l int) string {
return GenerateRandomString("0123456789abcdefghijklmnopqrstuvwxyz", l)
}
+27
View File
@@ -0,0 +1,27 @@
package l10n
var phrasesLT = map[string]string{
"Anonymous post": "Anoniminis įrašas",
"Blogs": "Tinklaraščiai",
"Enter": "Įvesti",
"Newer": "Naujesni",
"Older": "Senesni",
"Posts": "Įrašai",
"Publish to...": "Publikuoti į...",
"Publish": "Publikuoti",
"Read more...": "Skaityti daugiau...",
"This blog requires a password.": "Šis tinklaraštis reikalauja slaptažodžio.",
"Toggle theme": "Keisti temą",
"View posts": "Peržiūrėti įrašus",
"delete": "ištrinti",
"edit": "koreguoti",
"move to...": "perkelti į...",
"pin": "prisegti",
"published with write.as": "publikuojama su write.as",
"share modal ending": "Siųskite draugui, dalinkitės internete ar per Twitter. Sužinokite daugiau.",
"share modal introduction": "Kiekvienas publikuotas įrašas turi slaptą, unikalų URL kuriuo galite pasidalinti su bet kuo. Šis URL yra:",
"share modal title": "Dalintis šiuo įrašu",
"share": "dalintis",
"unpin": "atsegti",
"title dash": "–",
}
+8 -8
View File
@@ -3,24 +3,24 @@ package l10n
var phrasesZH = map[string]string{
"Anonymous post": "匿名文章",
"Blogs": "博客",
"Enter": "按下Enter按钮",
"Newer": "最近的博客",
"Older": "之前的博客",
"Enter": "按下回车",
"Newer": "更新",
"Older": "较旧",
"Posts": "文章",
"Publish to...": "发布到...",
"Publish": "发布",
"Read more...": "阅读更多",
"This blog requires a password.": "这篇博客需要密码",
"This blog requires a password.": "这篇文章需要密码",
"Toggle theme": "更换主题",
"View posts": "查看文章",
"delete": "删除",
"edit": "编辑",
"move to...": "移动到",
"pin": "固定文章",
"published with write.as": "write.as发布",
"share modal ending": "发送给朋友,或者线上分享,如果可能的话,分享到推特上。了解更多。",
"share modal introduction": "发布的每篇文章都有一个唯一的私有URL,你可以把它分享给任何人。这是URL",
"share modal title": "分享这篇文章",
"published with write.as": "通过 write.as 发布",
"share modal ending": "发送给朋友、与世界分享或者发条推。了解更多。",
"share modal introduction": "发布的每篇文章都有一个唯一的私有网址,你可以把它分享给任何人:",
"share modal title": "分享文章",
"share": "分享",
"unpin": "取消固定",
}
+2
View File
@@ -24,6 +24,8 @@ func Strings(lang string) map[string]string {
return phrasesIT
case "ja":
return phrasesJA
case "lt":
return phrasesLT
case "mk":
return phrasesMK
case "pl":
+17 -2
View File
@@ -2,8 +2,10 @@
package log
import (
"fmt"
"log"
"os"
"runtime"
)
var (
@@ -13,7 +15,7 @@ var (
func init() {
InfoLog = log.New(os.Stdout, "", log.Ldate|log.Ltime)
ErrorLog = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
ErrorLog = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime)
}
// Info logs an informational message to Stdout.
@@ -23,5 +25,18 @@ func Info(s string, v ...interface{}) {
// Error logs an error to Stderr.
func Error(s string, v ...interface{}) {
ErrorLog.Printf(s, v...)
// Include original caller information
_, file, line, _ := runtime.Caller(1)
// Determine short filename (from standard log package)
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
ErrorLog.Printf(fmt.Sprintf("%s:%d: ", short, line)+s, v...)
}
+10
View File
@@ -67,3 +67,13 @@ func (memo *Memo) Get() (value interface{}, err error) {
}
return memo.cache.res.value, memo.cache.res.err
}
// Reset forcibly resets the cache to nil without checking if the cache
// duration has elapsed.
func (memo *Memo) Reset() {
defer memo.mu.Unlock()
memo.mu.Lock()
memo.cache = nil
memo.lastCache = time.Now()
}
+7 -7
View File
@@ -1,4 +1,4 @@
// Package passgen generates random, unmemorable passwords.
// Package passgen generates random passwords.
//
// Example usage:
//
@@ -19,20 +19,20 @@ const DefLen = 20
// DefChars is the default set of characters used in the password.
var DefChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()- _=+,.?/:;{}[]`~")
// New returns a random password of the default length with the default set of
// characters.
// New returns a random, unmemorable password of the default length with the
// default set of characters.
func New() string {
return NewLenChars(DefLen, DefChars)
}
// NewLen returns a random password of the given length with the default set
// of characters.
// NewLen returns a random, unmemorable password of the given length with the
// default set of characters.
func NewLen(length int) string {
return NewLenChars(length, DefChars)
}
// NewLenChars returns a random password of the given length with the given
// set of characters.
// NewLenChars returns a random, unmemorable password of the given length with
// the given set of characters.
func NewLenChars(length int, chars []byte) string {
if length == 0 {
return ""
+64
View File
@@ -0,0 +1,64 @@
package passgen
import (
"crypto/rand"
"math/big"
)
var (
ar = []rune("aA4")
cr = []rune("cC")
er = []rune("eE3")
fr = []rune("fF")
gr = []rune("gG")
hr = []rune("hH")
ir = []rune("iI1")
lr = []rune("lL")
nr = []rune("nN")
or = []rune("oO0")
rr = []rune("rR")
sr = []rune("sS5")
tr = []rune("tT7")
remr = []rune("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ0123456789")
)
// NewWordish generates a password made of word-like words.
func NewWordish() string {
b := []rune{}
b = append(b, randLetter(cr))
b = append(b, randLetter(hr))
b = append(b, randLetter(ar))
b = append(b, randLetter(nr))
b = append(b, randLetter(gr))
b = append(b, randLetter(er))
b = append(b, randLetter(tr))
b = append(b, randLetter(hr))
b = append(b, randLetter(ir))
b = append(b, randLetter(sr))
b = append(b, randLetter(ar))
b = append(b, randLetter(fr))
b = append(b, randLetter(tr))
b = append(b, randLetter(er))
b = append(b, randLetter(rr))
b = append(b, randLetter(lr))
b = append(b, randLetter(or))
b = append(b, randLetter(gr))
b = append(b, randLetter(gr))
b = append(b, randLetter(ir))
b = append(b, randLetter(nr))
b = append(b, randLetter(gr))
b = append(b, randLetter(ir))
b = append(b, randLetter(nr))
for i := 0; i <= 7; i++ {
b = append(b, randLetter(remr))
}
return string(b)
}
func randLetter(l []rune) rune {
li, err := rand.Int(rand.Reader, big.NewInt(int64(len(l))))
if err != nil {
return rune(-1)
}
return l[li.Int64()]
}
+146
View File
@@ -1,9 +1,28 @@
package posts
import (
"fmt"
stripmd "github.com/writeas/go-strip-markdown"
"github.com/writeas/web-core/stringmanip"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
const (
maxTitleLen = 80
assumedTitleLen = 80
)
var (
titleElementReg = regexp.MustCompile("</?p>")
urlReg = regexp.MustCompile("https?://")
imgReg = regexp.MustCompile(`!\[([^]]+)\]\([^)]+\)`)
)
// ExtractTitle takes the given raw post text and returns a title, if explicitly
// provided, and a body.
func ExtractTitle(content string) (title string, body string) {
if hashIndex := strings.Index(content, "# "); hashIndex == 0 {
eol := strings.IndexRune(content, '\n')
@@ -17,3 +36,130 @@ func ExtractTitle(content string) (title string, body string) {
body = content
return
}
func FriendlyPostTitle(content, friendlyId string) string {
content = StripHTMLWithoutEscaping(content)
content = strings.TrimLeftFunc(stripmd.Strip(content), unicode.IsSpace)
eol := strings.IndexRune(content, '\n')
blankLine := strings.Index(content, "\n\n")
if blankLine != -1 && blankLine <= eol && blankLine <= assumedTitleLen {
return strings.TrimSpace(content[:blankLine])
} else if eol == -1 && utf8.RuneCountInString(content) <= maxTitleLen {
return content
}
title, truncd := TruncToWord(PostLede(content, true), maxTitleLen)
if truncd {
title += "..."
}
return title
}
// PostDescription generates a description based on the given post content,
// title, and post ID. This doesn't consider a V2 post field, `title` when
// choosing what to generate. In case a post has a title, this function will
// fail, and logic should instead be implemented to skip this when there's no
// title, like so:
// var desc string
// if title == "" {
// desc = PostDescription(content, title, friendlyId)
// } else {
// desc = ShortPostDescription(content)
// }
func PostDescription(content, title, friendlyId string) string {
maxLen := 140
if content == "" {
content = "WriteFreely is a painless, simple, federated blogging platform."
} else {
fmtStr := "%s"
truncation := 0
if utf8.RuneCountInString(content) > maxLen {
// Post is longer than the max description, so let's show a better description
fmtStr = "%s..."
truncation = 3
}
if title == friendlyId {
// No specific title was found; simply truncate the post, starting at the beginning
content = fmt.Sprintf(fmtStr, strings.Replace(stringmanip.Substring(content, 0, maxLen-truncation), "\n", " ", -1))
} else {
// There was a title, so return a real description
blankLine := strings.Index(content, "\n\n")
if blankLine < 0 {
blankLine = 0
}
truncd := stringmanip.Substring(content, blankLine, blankLine+maxLen-truncation)
contentNoNL := strings.Replace(truncd, "\n", " ", -1)
content = strings.TrimSpace(fmt.Sprintf(fmtStr, contentNoNL))
}
}
return content
}
func ShortPostDescription(content string) string {
maxLen := 140
fmtStr := "%s"
truncation := 0
if utf8.RuneCountInString(content) > maxLen {
// Post is longer than the max description, so let's show a better description
fmtStr = "%s..."
truncation = 3
}
return strings.TrimSpace(fmt.Sprintf(fmtStr, strings.Replace(stringmanip.Substring(content, 0, maxLen-truncation), "\n", " ", -1)))
}
// TruncToWord truncates the given text to the provided limit.
func TruncToWord(s string, l int) (string, bool) {
truncated := false
c := []rune(s)
if len(c) > l {
truncated = true
s = string(c[:l])
spaceIdx := strings.LastIndexByte(s, ' ')
if spaceIdx > -1 {
s = s[:spaceIdx]
}
}
return s, truncated
}
// PostLede attempts to extract the first thought of the given post, generally
// contained within the first line or sentence of text.
func PostLede(t string, includePunc bool) string {
// Adjust where we truncate if we want to include punctuation
iAdj := 0
if includePunc {
iAdj = 1
}
// Find lede within first line of text
nl := strings.IndexRune(t, '\n')
if nl > -1 {
t = t[:nl]
}
// Strip certain HTML tags
t = titleElementReg.ReplaceAllString(t, "")
// Strip URL protocols
t = urlReg.ReplaceAllString(t, "")
// Strip image URL, leaving only alt text
t = imgReg.ReplaceAllString(t, " $1 ")
// Find lede within first sentence
punc := strings.Index(t, ". ")
if punc > -1 {
t = t[:punc+iAdj]
}
punc = stringmanip.IndexRune(t, '。')
if punc > -1 {
c := []rune(t)
t = string(c[:punc+iAdj])
}
return t
}
+7
View File
@@ -3,6 +3,7 @@ package posts
import (
"github.com/microcosm-cc/bluemonday"
"github.com/writeas/saturday"
"html"
"regexp"
"strings"
"unicode"
@@ -61,3 +62,9 @@ func ApplyBasicMarkdown(data []byte) string {
return outHTML
}
// StripHTMLWithoutEscaping strips HTML tags with bluemonday's StrictPolicy, then unescapes the HTML
// entities added in by sanitizing the content.
func StripHTMLWithoutEscaping(content string) string {
return html.UnescapeString(bluemonday.StrictPolicy().Sanitize(content))
}
+29
View File
@@ -0,0 +1,29 @@
package silobridge
// fakeAPInstances contains a list of sites that we allow writers to mention
// with the @handle@instance.tld syntax, plus the corresponding prefix to
// insert between `https://instance.tld/` and `handle` (e.g.
// https://medium.com/@handle)
var fakeAPInstances = map[string]string{
"deviantart.com": "",
"facebook.com": "",
"flickr.com": "photos/",
"github.com": "",
"instagram.com": "",
"medium.com": "@",
"reddit.com": "user/",
"twitter.com": "",
"wattpad.com": "user/",
"youtube.com": "user/",
}
// Profile returns the full profile URL for a fake ActivityPub instance, based
// on the given handle and domain. If the domain isn't recognized, an empty
// string is returned.
func Profile(handle, domain string) string {
prefix, ok := fakeAPInstances[domain]
if !ok {
return ""
}
return "https://" + domain + "/" + prefix + handle
}