Make Link prefixes configurable.

This commit is contained in:
Sven Windisch 2021-12-12 22:31:04 +01:00
parent 63fc92a654
commit 0a4a4697ff
2 changed files with 77 additions and 8 deletions

View file

@ -20,6 +20,7 @@ import (
// "bytes"
"errors"
"fmt"
// "html"
"regexp"
"sort"
@ -37,6 +38,11 @@ type Token struct {
TPipes []string `json:"tPipes,omitempty"`
}
type Prefixes []string
var ExtLinkPrefixes Prefixes = []string{"https://", "http://", "ftp://", "//"}
var FileLinkPrefixes Prefixes = []string{"[[image:", "[[media:", "[[file:"}
func (a *Article) parseRedirectLine(l string) ([]*Token, error) {
nt := make([]*Token, 0, 2)
nt = append(nt, &Token{TType: "redirect"})
@ -220,18 +226,12 @@ func matchPrefixes(s string, prefixes []string) bool {
return false
}
var extlinkre = regexp.MustCompile(`^(http:)|(ftp:)|()//[^\s]+`)
func isExtLink(l string) bool {
// return extlinkre.MatchString(l)
return matchPrefixes(l, []string{"http://", "ftp://", "//"})
return matchPrefixes(l, ExtLinkPrefixes)
}
var filelinkre = regexp.MustCompile(`(?i)^\[\[(?:image:)|(?:media:)|(?:file:)`)
func possibleFileLink(l string) bool {
// return filelinkre.MatchString(l)
return matchPrefixes(l, []string{"[[image:", "[[media:", "[[file:"})
return matchPrefixes(l, FileLinkPrefixes)
}
func (a *Article) parseLink(l string) (int, []*Token, bool) {

69
tokenize_test.go Normal file
View file

@ -0,0 +1,69 @@
/*
Copyright (C) Sven Windisch <semantosoph@posteo.de>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gowiki
import (
"testing"
)
func TestMediaLink(t *testing.T) {
mw := "[[File:test.png]]"
t.Log(mw)
a, err := ParseArticle("Test", mw, &DummyPageGetter{})
if err != nil {
t.Error("Error:", err)
}
l := a.GetMedia()
if l[0].Namespace != "File" || l[0].PageName != "Test.png" {
t.Error("Error parsing media link ", l)
}
}
func TestCustomMediaLink(t *testing.T) {
mw := "[[Datei:test.jpg]]"
t.Log(mw)
StandardNamespaces["datei"] = "Datei"
FileLinkPrefixes = append(FileLinkPrefixes, "[[datei:")
a, err := ParseArticle("Test", mw, &DummyPageGetter{})
if err != nil {
t.Error("Error:", err)
}
l := a.GetMedia()
if l[0].Namespace != "Datei" || l[0].PageName != "Test.jpg" {
t.Error("Error parsing media link ", l)
}
}
func TestExternalLink(t *testing.T) {
mw := "[[https://test.org|Test]]"
t.Log(mw)
a, err := ParseArticle("Test", mw, &DummyPageGetter{})
if err != nil {
t.Error("Error:", err)
}
l := a.GetTextLinks()
if l[0].Text != "Test" || l[0].Link.PageName != "Https://test.org" {
t.Error("Error parsing media link ", l)
}
}