2015-12-10 22:19:38 +00:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-02-16 09:48:15 +00:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-17 23:36:48 +00:00
|
|
|
"io"
|
2015-02-16 09:48:15 +00:00
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
type matchState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
matchStateNone matchState = iota
|
|
|
|
matchStateWhitespace
|
|
|
|
matchStatePartial
|
|
|
|
matchStateFull
|
|
|
|
)
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
type absurllexer struct {
|
|
|
|
// the source to absurlify
|
2015-02-16 09:48:15 +00:00
|
|
|
content []byte
|
2015-05-04 10:51:48 +00:00
|
|
|
// the target for the new absurlified content
|
|
|
|
w io.Writer
|
2015-02-16 09:48:15 +00:00
|
|
|
|
2015-05-15 22:11:39 +00:00
|
|
|
// path may be set to a "." relative path
|
|
|
|
path []byte
|
|
|
|
|
2015-03-10 17:44:32 +00:00
|
|
|
pos int // input position
|
|
|
|
start int // item start position
|
|
|
|
width int // width of last element
|
2015-02-16 09:48:15 +00:00
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
matchers []absURLMatcher
|
|
|
|
|
|
|
|
ms matchState
|
|
|
|
matches [3]bool // track matches of the 3 prefixes
|
2015-05-03 22:06:10 +00:00
|
|
|
idx int // last index in matches checked
|
2015-02-16 09:48:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
type stateFunc func(*absurllexer) stateFunc
|
2015-02-16 09:48:15 +00:00
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
// prefix is how to identify and which func to handle the replacement.
|
2015-05-03 17:54:17 +00:00
|
|
|
type prefix struct {
|
|
|
|
r []rune
|
2015-05-04 10:51:48 +00:00
|
|
|
f func(l *absurllexer)
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
2015-02-16 09:48:15 +00:00
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
// new prefixes can be added below, but note:
|
|
|
|
// - the matches array above must be expanded.
|
|
|
|
// - the prefix must with the current logic end with '='
|
2015-05-03 17:54:17 +00:00
|
|
|
var prefixes = []*prefix{
|
2015-05-25 10:54:12 +00:00
|
|
|
{r: []rune{'s', 'r', 'c', '='}, f: checkCandidateBase},
|
|
|
|
{r: []rune{'h', 'r', 'e', 'f', '='}, f: checkCandidateBase},
|
|
|
|
{r: []rune{'s', 'r', 'c', 's', 'e', 't', '='}, f: checkCandidateSrcset},
|
2015-05-03 20:42:56 +00:00
|
|
|
}
|
2015-02-16 09:48:15 +00:00
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
type absURLMatcher struct {
|
2015-05-15 22:11:39 +00:00
|
|
|
match []byte
|
|
|
|
quote []byte
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
// match check rune inside word. Will be != ' '.
|
|
|
|
func (l *absurllexer) match(r rune) {
|
2015-05-03 17:54:17 +00:00
|
|
|
|
|
|
|
var found bool
|
|
|
|
|
|
|
|
// note, the prefixes can start off on the same foot, i.e.
|
|
|
|
// src and srcset.
|
|
|
|
if l.ms == matchStateWhitespace {
|
2015-05-03 22:06:10 +00:00
|
|
|
l.idx = 0
|
2015-05-03 17:54:17 +00:00
|
|
|
for j, p := range prefixes {
|
2015-05-03 22:06:10 +00:00
|
|
|
if r == p.r[l.idx] {
|
2015-05-03 17:54:17 +00:00
|
|
|
l.matches[j] = true
|
|
|
|
found = true
|
2016-02-06 19:50:26 +00:00
|
|
|
// checkMatchState will only return true when r=='=', so
|
|
|
|
// we can safely ignore the return value here.
|
|
|
|
l.checkMatchState(r, j)
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-03 17:54:17 +00:00
|
|
|
|
|
|
|
if !found {
|
|
|
|
l.ms = matchStateNone
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-03 22:06:10 +00:00
|
|
|
l.idx++
|
2015-05-03 17:54:17 +00:00
|
|
|
for j, m := range l.matches {
|
|
|
|
// still a match?
|
|
|
|
if m {
|
2015-05-03 22:06:10 +00:00
|
|
|
if prefixes[j].r[l.idx] == r {
|
2015-05-03 17:54:17 +00:00
|
|
|
found = true
|
|
|
|
if l.checkMatchState(r, j) {
|
|
|
|
return
|
|
|
|
}
|
2015-02-16 09:48:15 +00:00
|
|
|
} else {
|
2015-05-03 17:54:17 +00:00
|
|
|
l.matches[j] = false
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
2015-02-16 09:48:15 +00:00
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
if !found {
|
|
|
|
l.ms = matchStateNone
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
func (l *absurllexer) checkMatchState(r rune, idx int) bool {
|
2015-05-03 17:54:17 +00:00
|
|
|
if r == '=' {
|
|
|
|
l.ms = matchStateFull
|
|
|
|
for k := range l.matches {
|
|
|
|
if k != idx {
|
|
|
|
l.matches[k] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
l.ms = matchStatePartial
|
|
|
|
|
|
|
|
return false
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
func (l *absurllexer) emit() {
|
2015-03-17 23:36:48 +00:00
|
|
|
l.w.Write(l.content[l.start:l.pos])
|
2015-02-16 09:48:15 +00:00
|
|
|
l.start = l.pos
|
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
// handle URLs in src and href.
|
|
|
|
func checkCandidateBase(l *absurllexer) {
|
2015-05-03 17:54:17 +00:00
|
|
|
for _, m := range l.matchers {
|
2015-05-03 20:42:56 +00:00
|
|
|
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
|
2015-05-03 17:54:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-05-03 20:42:56 +00:00
|
|
|
// check for schemaless URLs
|
|
|
|
posAfter := l.pos + len(m.match)
|
|
|
|
if posAfter >= len(l.content) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r, _ := utf8.DecodeRune(l.content[posAfter:])
|
|
|
|
if r == '/' {
|
|
|
|
// schemaless: skip
|
|
|
|
return
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
2015-05-03 20:42:56 +00:00
|
|
|
if l.pos > l.start {
|
|
|
|
l.emit()
|
|
|
|
}
|
|
|
|
l.pos += len(m.match)
|
|
|
|
l.w.Write(m.quote)
|
2015-05-15 22:11:39 +00:00
|
|
|
l.w.Write(l.path)
|
2015-05-03 20:42:56 +00:00
|
|
|
l.start = l.pos
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
// handle URLs in srcset.
|
|
|
|
func checkCandidateSrcset(l *absurllexer) {
|
2015-05-03 17:54:17 +00:00
|
|
|
// special case, not frequent (me think)
|
2015-03-10 17:44:32 +00:00
|
|
|
for _, m := range l.matchers {
|
2015-05-03 17:54:17 +00:00
|
|
|
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
|
2015-03-10 17:44:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
// check for schemaless URLs
|
|
|
|
posAfter := l.pos + len(m.match)
|
|
|
|
if posAfter >= len(l.content) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r, _ := utf8.DecodeRune(l.content[posAfter:])
|
|
|
|
if r == '/' {
|
|
|
|
// schemaless: skip
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
posLastQuote := bytes.Index(l.content[l.pos+1:], m.quote)
|
|
|
|
|
|
|
|
// safe guard
|
|
|
|
if posLastQuote < 0 || posLastQuote > 2000 {
|
2015-03-10 17:44:32 +00:00
|
|
|
return
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
2015-03-10 17:44:32 +00:00
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
if l.pos > l.start {
|
|
|
|
l.emit()
|
2015-03-10 17:44:32 +00:00
|
|
|
}
|
2015-05-03 17:54:17 +00:00
|
|
|
|
|
|
|
section := l.content[l.pos+len(m.quote) : l.pos+posLastQuote+1]
|
|
|
|
|
|
|
|
fields := bytes.Fields(section)
|
2016-03-14 19:31:31 +00:00
|
|
|
l.w.Write(m.quote)
|
2015-05-03 17:54:17 +00:00
|
|
|
for i, f := range fields {
|
|
|
|
if f[0] == '/' {
|
2015-05-15 22:11:39 +00:00
|
|
|
l.w.Write(l.path)
|
2015-05-03 17:54:17 +00:00
|
|
|
l.w.Write(f[1:])
|
|
|
|
|
|
|
|
} else {
|
|
|
|
l.w.Write(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < len(fields)-1 {
|
|
|
|
l.w.Write([]byte(" "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.w.Write(m.quote)
|
|
|
|
l.pos += len(section) + (len(m.quote) * 2)
|
|
|
|
l.start = l.pos
|
2015-03-10 17:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
// main loop
|
|
|
|
func (l *absurllexer) replace() {
|
2015-02-16 09:48:15 +00:00
|
|
|
contentLength := len(l.content)
|
|
|
|
var r rune
|
|
|
|
|
|
|
|
for {
|
2015-03-18 19:18:18 +00:00
|
|
|
if l.pos >= contentLength {
|
2015-02-16 09:48:15 +00:00
|
|
|
l.width = 0
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
var width = 1
|
2015-02-16 09:48:15 +00:00
|
|
|
r = rune(l.content[l.pos])
|
|
|
|
if r >= utf8.RuneSelf {
|
|
|
|
r, width = utf8.DecodeRune(l.content[l.pos:])
|
|
|
|
}
|
2015-03-10 17:44:32 +00:00
|
|
|
l.width = width
|
2015-02-16 09:48:15 +00:00
|
|
|
l.pos += l.width
|
|
|
|
if r == ' ' {
|
2015-05-03 17:54:17 +00:00
|
|
|
l.ms = matchStateWhitespace
|
|
|
|
} else if l.ms != matchStateNone {
|
2015-02-16 09:48:15 +00:00
|
|
|
l.match(r)
|
2015-05-03 17:54:17 +00:00
|
|
|
if l.ms == matchStateFull {
|
|
|
|
var p *prefix
|
|
|
|
for i, m := range l.matches {
|
|
|
|
if m {
|
|
|
|
p = prefixes[i]
|
2015-05-04 10:51:48 +00:00
|
|
|
l.matches[i] = false
|
2015-05-03 17:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
l.ms = matchStateNone
|
|
|
|
p.f(l)
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Done!
|
|
|
|
if l.pos > l.start {
|
2015-03-10 17:44:32 +00:00
|
|
|
l.emit()
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 00:55:49 +00:00
|
|
|
func doReplace(ct contentTransformer, matchers []absURLMatcher) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
2015-05-04 10:51:48 +00:00
|
|
|
lexer := &absurllexer{
|
2015-05-03 17:54:17 +00:00
|
|
|
content: ct.Content(),
|
|
|
|
w: ct,
|
2015-05-15 22:11:39 +00:00
|
|
|
path: ct.Path(),
|
2015-05-03 17:54:17 +00:00
|
|
|
matchers: matchers}
|
2015-02-17 03:33:44 +00:00
|
|
|
|
2015-03-10 17:44:32 +00:00
|
|
|
lexer.replace()
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
type absURLReplacer struct {
|
|
|
|
htmlMatchers []absURLMatcher
|
|
|
|
xmlMatchers []absURLMatcher
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:11:39 +00:00
|
|
|
func newAbsURLReplacer() *absURLReplacer {
|
2015-02-16 09:48:15 +00:00
|
|
|
|
|
|
|
// HTML
|
2015-03-11 17:34:57 +00:00
|
|
|
dqHTMLMatch := []byte("\"/")
|
|
|
|
sqHTMLMatch := []byte("'/")
|
2015-02-16 09:48:15 +00:00
|
|
|
|
|
|
|
// XML
|
2015-03-11 17:34:57 +00:00
|
|
|
dqXMLMatch := []byte(""/")
|
|
|
|
sqXMLMatch := []byte("'/")
|
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
dqHTML := []byte("\"")
|
|
|
|
sqHTML := []byte("'")
|
2015-03-11 17:34:57 +00:00
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
dqXML := []byte(""")
|
|
|
|
sqXML := []byte("'")
|
2015-03-11 17:34:57 +00:00
|
|
|
|
|
|
|
return &absURLReplacer{
|
|
|
|
htmlMatchers: []absURLMatcher{
|
2015-05-15 22:11:39 +00:00
|
|
|
{dqHTMLMatch, dqHTML},
|
|
|
|
{sqHTMLMatch, sqHTML},
|
2015-05-03 21:04:38 +00:00
|
|
|
},
|
2015-03-11 17:34:57 +00:00
|
|
|
xmlMatchers: []absURLMatcher{
|
2015-05-15 22:11:39 +00:00
|
|
|
{dqXMLMatch, dqXML},
|
|
|
|
{sqXMLMatch, sqXML},
|
2015-02-16 09:48:15 +00:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2015-03-19 00:55:49 +00:00
|
|
|
func (au *absURLReplacer) replaceInHTML(ct contentTransformer) {
|
|
|
|
doReplace(ct, au.htmlMatchers)
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 00:55:49 +00:00
|
|
|
func (au *absURLReplacer) replaceInXML(ct contentTransformer) {
|
|
|
|
doReplace(ct, au.xmlMatchers)
|
2015-02-16 09:48:15 +00:00
|
|
|
}
|