9 Commits
Author SHA1 Message Date
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
11 changed files with 250 additions and 1 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ type (
}
Endpoints struct {
SharedInbox string `json:"sharedInbox"`
SharedInbox string `json:"sharedInbox,omitempty"`
}
Image struct {
+21
View File
@@ -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)
}
+36
View File
@@ -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
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": "share",
"unpin": "unpin",
"title dash": "&mdash;",
}
+1
View File
@@ -23,4 +23,5 @@ var phrasesDE = map[string]string{
"share modal title": "Teile diesen Beitrag",
"share": "Teilen",
"unpin": "Lösen",
"title dash": "&ndash;",
}
+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
}