Ejemplo n.º 1
0
// If rule has slugs, then dat will already contain it if it was sent from UI.
func Slug(rule map[string]interface{}, dat map[string]interface{}) {
	_, has_slug := rule["slug"]
	if has_slug {
		if slug, has := dat["slug"]; has && len(slug.(string)) > 0 {
			return
		} else if name, has_name := dat["name"]; has_name {
			dat["slug"] = slugify.S(name.(string))
		} else if title, has_title := dat["title"]; has_title {
			dat["slug"] = slugify.S(title.(string))
		}
	}
}
Ejemplo n.º 2
0
func GenerateKeywords(s string) []string {
	split := strings.Split(s, " ")
	slugified := []string{}
	for _, v := range split {
		slugified = append(slugified, strings.Trim(slugify.S(v), ",.:;"))
	}
	return slugified
}
Ejemplo n.º 3
0
// Returns true if the username is still available.
func NameAvailable(a iface.Filter, name string) (bool, error) {
	q := bson.M{"slug": slugify.S(name)}
	users, err := a.AddQuery(q).Find()
	if err != nil {
		return false, err
	}
	if len(users) > 0 {
		return false, nil
	}
	return true, nil
}
Ejemplo n.º 4
0
func simpleFulltext(non_split []string) []string {
	split := []string{}
	for _, v := range non_split {
		split = append(split, strings.Split(v, " ")...)
	}
	slugified := []string{}
	for _, v := range split {
		slugified = append(slugified, strings.Trim(slugify.S(v), ",.:;"))
	}
	slugified = filterDupes(slugified)
	return filterTooShort(slugified, 3)
}
Ejemplo n.º 5
0
// Returns true if the username is still available (eg: no one is registered with that name).
func NameAvailable(db *mgo.Database, name string) (bool, error) {
	var res []interface{}
	q := bson.M{"slug": slugify.S(name)}
	err := db.C("users").Find(q).All(&res)
	if err != nil {
		return false, err
	}
	if len(res) > 0 {
		return false, nil
	}
	return true, nil
}
Ejemplo n.º 6
0
func insertTags(db *mgo.Database, tagnames []string) []bson.ObjectId {
	ret := []bson.ObjectId{}
	for _, v := range tagnames {
		if len(v) == 0 {
			continue
		}
		id := bson.NewObjectId()
		slug := slugify.S(v)
		tag := m{"_id": id, "slug": slug, "name": v, Count_fieldname: 0}
		db.C(Tag_cname).Insert(tag)
		ret = append(ret, id)
	}
	return ret
}
Ejemplo n.º 7
0
// Creates slugs from the tagnames, queries all tags which exists in the database with those slugs.
// Returns the ids of the existing tags, and returns all tagnames which is not in the database.
func separateTags(db *mgo.Database, tagnames []string) ([]bson.ObjectId, []string) {
	var i []interface{}
	slugs := []string{}
	for _, val := range tagnames {
		slug := slugify.S(val)
		slugs = append(slugs, slug)
	}
	db.C(Tag_cname).Find(m{"slug": m{"$in": slugs}}).Limit(0).All(&i)
	ret_ids := []bson.ObjectId{}
	contains := createM(slugs, tagnames)
	i = basic.Convert(i).([]interface{})
	for _, v := range i {
		val := v.(map[string]interface{})
		ret_ids = append(ret_ids, val["_id"].(bson.ObjectId))
		delete(contains, val["slug"].(string))
	}
	return ret_ids, mToSSlice(contains)
}
Ejemplo n.º 8
0
// Registers a normal user with password and level 100.
// See RegisterGuest for an other kind of registration.
// See admin_model for registrations of admins.
func RegisterUser(db *mgo.Database, ev ifaces.Event, rules map[string]interface{}, inp map[string][]string) (bson.ObjectId, error) {
	userDefaults(rules)
	user, err := extract.New(rules).Extract(inp)
	if err != nil {
		return "", err
	}
	if user["password"].(string) != user["password_again"].(string) {
		return "", fmt.Errorf("Password and password confirmation differs.")
	}
	delete(user, "password_again")
	user["password"] = EncodePass(user["password"].(string))
	user["slug"] = slugify.S(user["slug"].(string))
	user["level"] = 100
	user_id := bson.NewObjectId()
	user["_id"] = user_id
	err = db.C("users").Insert(user)
	if err != nil {
		return "", fmt.Errorf("Name is not unique.")
	}
	delete(user, "password")
	ev.Trigger("user.register", user)
	return user_id, nil
}
Ejemplo n.º 9
0
func main() {
	str := `This is some hungarian text with accents: "Helló, belló."`
	fmt.Println(slugify.S(str))
}