11 Commits
Author SHA1 Message Date
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
Matt Baer 4cb17a6518 Add title dash
This defaults to the em dash, but uses an en dash on German posts.

Part of writeas/writefreely#1
2018-12-24 13:48:21 -05:00
Matt Baer 05f387ffa1 Omit empty endpoints.sharedInbox
This fixes writeas/writefreely#8
2018-11-11 11:55:28 -05:00
Matt Baer 265880c2cf Add tags package documentation 2018-10-28 12:01:02 -04:00
Matt Baer 316410bd35 Don't fail test when repeat slug generated 2018-10-16 23:21:12 -04:00
Matt Baer 3d47537e1f Add stringmanip package 2018-10-16 22:58:06 -04:00
Matt Baer 567e0c6ef0 Add i18n.LangIsRTL func 2018-10-16 20:38:27 -04:00
Matt Baer 73bffef9af Add tags.Extract() func 2018-10-16 20:33:34 -04:00
Matt Baer e5a5d63c6f Add Convert(JSON|SQL) funcs 2018-10-16 18:26:08 -04:00
Matt Baer 6c98a50085 Add safe slug generation func in package id 2018-10-15 17:41:41 -04:00
14 changed files with 322 additions and 9 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ Write.as web core
================= =================
[![GoDoc](https://godoc.org/github.com/writeas/web-core?status.svg)](https://godoc.org/github.com/writeas/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) [![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) [![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 [Write.as](https://write.as) web application.
+1 -1
View File
@@ -17,7 +17,7 @@ type (
} }
Endpoints struct { Endpoints struct {
SharedInbox string `json:"sharedInbox"` SharedInbox string `json:"sharedInbox,omitempty"`
} }
Image struct { Image struct {
+21
View File
@@ -80,3 +80,24 @@ func (v *NullJSONString) UnmarshalJSON(data []byte) error {
} }
return nil return nil
} }
func ConvertJSONNullString(value string) reflect.Value {
v := NullJSONString{}
if err := v.Scan(value); err != nil {
return reflect.Value{}
}
return reflect.ValueOf(v)
}
func ConvertJSONNullBool(value string) reflect.Value {
v := NullJSONBool{}
if value == "on" || value == "off" {
return reflect.ValueOf(NullJSONBool{sql.NullBool{Bool: value == "on", Valid: true}})
}
if err := v.Scan(value); err != nil {
return reflect.Value{}
}
return reflect.ValueOf(v)
}
+36
View File
@@ -40,3 +40,39 @@ func SQLNullFloat64(value string) reflect.Value {
return reflect.ValueOf(v) return reflect.ValueOf(v)
} }
func ConvertSQLNullString(value string) reflect.Value {
v := sql.NullString{}
if err := v.Scan(value); err != nil {
return reflect.Value{}
}
return reflect.ValueOf(v)
}
func ConvertSQLNullBool(value string) reflect.Value {
v := sql.NullBool{}
if err := v.Scan(value); err != nil {
return reflect.Value{}
}
return reflect.ValueOf(v)
}
func ConvertSQLNullInt64(value string) reflect.Value {
v := sql.NullInt64{}
if err := v.Scan(value); err != nil {
return reflect.Value{}
}
return reflect.ValueOf(v)
}
func ConvertSQLNullFloat64(value string) reflect.Value {
v := sql.NullFloat64{}
if err := v.Scan(value); err != nil {
return reflect.Value{}
}
return reflect.ValueOf(v)
}
+21
View File
@@ -0,0 +1,21 @@
package i18n
var rtlLangs = map[string]bool{
"ar": true, // Arabic
"dv": true, // Divehi
"fa": true, // Persian (Farsi)
"ha": true, // Hausa
"he": true, // Hebrew
"iw": true, // Hebrew (old code)
"ji": true, // Yiddish (old code)
"ps": true, // Pashto, Pushto
"ur": true, // Urdu
"yi": true, // Yiddish
}
func LangIsRTL(lang string) bool {
if _, ok := rtlLangs[lang]; ok {
return true
}
return false
}
+12
View File
@@ -0,0 +1,12 @@
package id
import (
"fmt"
"github.com/writeas/nerds/store"
)
// 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))
}
+19
View File
@@ -0,0 +1,19 @@
package id
import "testing"
func TestGenSafeUniqueSlug(t *testing.T) {
slug := "slug"
r := map[string]bool{}
for i := 0; i < 1000; i++ {
s := GenSafeUniqueSlug(slug)
if s == slug {
t.Errorf("Got same slug as inputted!")
}
if _, ok := r[s]; ok {
t.Logf("#%d: slug %s was already generated in testing.", i, s)
}
r[s] = true
}
}
+1
View File
@@ -32,4 +32,5 @@ var phrases = map[string]string{
"share modal title": "Share this post", "share modal title": "Share this post",
"share": "share", "share": "share",
"unpin": "unpin", "unpin": "unpin",
"title dash": "&mdash;",
} }
+1
View File
@@ -23,4 +23,5 @@ var phrasesDE = map[string]string{
"share modal title": "Teile diesen Beitrag", "share modal title": "Teile diesen Beitrag",
"share": "Teilen", "share": "Teilen",
"unpin": "Lösen", "unpin": "Lösen",
"title dash": "&ndash;",
} }
+7 -7
View File
@@ -1,4 +1,4 @@
// Package passgen generates random, unmemorable passwords. // Package passgen generates random passwords.
// //
// Example usage: // Example usage:
// //
@@ -19,20 +19,20 @@ const DefLen = 20
// DefChars is the default set of characters used in the password. // DefChars is the default set of characters used in the password.
var DefChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()- _=+,.?/:;{}[]`~") var DefChars = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()- _=+,.?/:;{}[]`~")
// New returns a random password of the default length with the default set of // New returns a random, unmemorable password of the default length with the
// characters. // default set of characters.
func New() string { func New() string {
return NewLenChars(DefLen, DefChars) return NewLenChars(DefLen, DefChars)
} }
// NewLen returns a random password of the given length with the default set // NewLen returns a random, unmemorable password of the given length with the
// of characters. // default set of characters.
func NewLen(length int) string { func NewLen(length int) string {
return NewLenChars(length, DefChars) return NewLenChars(length, DefChars)
} }
// NewLenChars returns a random password of the given length with the given // NewLenChars returns a random, unmemorable password of the given length with
// set of characters. // the given set of characters.
func NewLenChars(length int, chars []byte) string { func NewLenChars(length int, chars []byte) string {
if length == 0 { if length == 0 {
return "" 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()]
}
+96
View File
@@ -0,0 +1,96 @@
package stringmanip
/*
The MIT License (MIT)
Copyright (c) 2016 Sergey Kamardin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Source: https://github.com/gobwas/glob/blob/master/util/runes/runes.go
func IndexRune(str string, r rune) int {
s := []rune(str)
for i, c := range s {
if c == r {
return i
}
}
return -1
}
func LastIndexRune(str string, needle rune) int {
s := []rune(str)
needles := []rune{needle}
ls, ln := len(s), len(needles)
switch {
case ln == 0:
if ls == 0 {
return 0
}
return ls
case ln == 1:
return IndexLastRune(s, needles[0])
case ln == ls:
if EqualRunes(s, needles) {
return 0
}
return -1
case ln > ls:
return -1
}
head:
for i := ls - 1; i >= 0 && i >= ln; i-- {
for y := ln - 1; y >= 0; y-- {
if s[i-(ln-y-1)] != needles[y] {
continue head
}
}
return i - ln + 1
}
return -1
}
func IndexLastRune(s []rune, r rune) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == r {
return i
}
}
return -1
}
func EqualRunes(a, b []rune) bool {
if len(a) == len(b) {
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
return false
}
+17
View File
@@ -0,0 +1,17 @@
package stringmanip
// Substring provides a safe way to extract a substring from a UTF-8 string.
// From this discussion:
// https://groups.google.com/d/msg/golang-nuts/cGq1Irv_5Vs/0SKoj49BsWQJ
func Substring(s string, p, l int) string {
if p < 0 || l <= 0 {
return ""
}
c := []rune(s)
if p > len(c) {
return ""
} else if p+l > len(c) || p+l < p {
return string(c[p:])
}
return string(c[p : p+l])
}
+25
View File
@@ -0,0 +1,25 @@
// Package tags supports operations around hashtags in plain text content
package tags
import (
"github.com/kylemcc/twitter-text-go/extract"
)
// Extract finds all hashtags in the given string and returns a de-duplicated
// list of them.
func Extract(body string) []string {
matches := extract.ExtractHashtags(body)
tags := map[string]bool{}
for i := range matches {
// Second value (whether or not there's a hashtag) ignored here, since
// we're only extracting hashtags.
ht, _ := matches[i].Hashtag()
tags[ht] = true
}
resTags := make([]string, 0)
for k := range tags {
resTags = append(resTags, k)
}
return resTags
}