Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ce8cb5ee19 | ||
|
|
5631982c2b | ||
|
|
4cb17a6518 | ||
|
|
05f387ffa1 | ||
|
|
265880c2cf | ||
|
|
316410bd35 | ||
|
|
3d47537e1f | ||
|
|
567e0c6ef0 | ||
|
|
73bffef9af | ||
|
|
e5a5d63c6f | ||
|
|
6c98a50085 |
@@ -2,7 +2,7 @@ Write.as web core
|
||||
=================
|
||||
[](https://godoc.org/github.com/writeas/web-core)
|
||||
[](https://travis-ci.org/writeas/web-core)
|
||||
[](http://slack.write.as/)
|
||||
[](http://webchat.freenode.net/?channels=writefreely)
|
||||
[](https://discuss.write.as/c/development)
|
||||
|
||||
web-core holds components of the [Write.as](https://write.as) web application.
|
||||
|
||||
@@ -17,7 +17,7 @@ type (
|
||||
}
|
||||
|
||||
Endpoints struct {
|
||||
SharedInbox string `json:"sharedInbox"`
|
||||
SharedInbox string `json:"sharedInbox,omitempty"`
|
||||
}
|
||||
|
||||
Image struct {
|
||||
|
||||
@@ -80,3 +80,24 @@ func (v *NullJSONString) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -40,3 +40,39 @@ func SQLNullFloat64(value string) reflect.Value {
|
||||
|
||||
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
@@ -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
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -32,4 +32,5 @@ var phrases = map[string]string{
|
||||
"share modal title": "Share this post",
|
||||
"share": "share",
|
||||
"unpin": "unpin",
|
||||
"title dash": "—",
|
||||
}
|
||||
|
||||
@@ -23,4 +23,5 @@ var phrasesDE = map[string]string{
|
||||
"share modal title": "Teile diesen Beitrag",
|
||||
"share": "Teilen",
|
||||
"unpin": "Lösen",
|
||||
"title dash": "–",
|
||||
}
|
||||
|
||||
+7
-7
@@ -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 ""
|
||||
|
||||
@@ -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()]
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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])
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user