Skip to content

Commit

Permalink
Remove sourceRelativeLinks
Browse files Browse the repository at this point in the history
Fixes #3766
  • Loading branch information
bep committed Aug 2, 2017
1 parent 481924b commit 9891c0f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 329 deletions.
53 changes: 20 additions & 33 deletions helpers/content.go
Expand Up @@ -62,35 +62,31 @@ func NewContentSpec(cfg config.Provider) *ContentSpec {

// Blackfriday holds configuration values for Blackfriday rendering.
type Blackfriday struct {
Smartypants bool
SmartypantsQuotesNBSP bool
AngledQuotes bool
Fractions bool
HrefTargetBlank bool
SmartDashes bool
LatexDashes bool
TaskLists bool
PlainIDAnchors bool
SourceRelativeLinksEval bool
SourceRelativeLinksProjectFolder string
Extensions []string
ExtensionsMask []string
Smartypants bool
SmartypantsQuotesNBSP bool
AngledQuotes bool
Fractions bool
HrefTargetBlank bool
SmartDashes bool
LatexDashes bool
TaskLists bool
PlainIDAnchors bool
Extensions []string
ExtensionsMask []string
}

// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
func (c ContentSpec) NewBlackfriday() *Blackfriday {
defaultParam := map[string]interface{}{
"smartypants": true,
"angledQuotes": false,
"smartypantsQuotesNBSP": false,
"fractions": true,
"hrefTargetBlank": false,
"smartDashes": true,
"latexDashes": true,
"plainIDAnchors": true,
"taskLists": true,
"sourceRelativeLinks": false,
"sourceRelativeLinksProjectFolder": "/docs/content",
"smartypants": true,
"angledQuotes": false,
"smartypantsQuotesNBSP": false,
"fractions": true,
"hrefTargetBlank": false,
"smartDashes": true,
"latexDashes": true,
"plainIDAnchors": true,
"taskLists": true,
}

ToLowerMap(defaultParam)
Expand All @@ -112,13 +108,6 @@ func (c ContentSpec) NewBlackfriday() *Blackfriday {
jww.FATAL.Printf("Failed to get site rendering config\n%s", err.Error())
}

if combinedConfig.SourceRelativeLinksEval {
// Remove in Hugo 0.21
Deprecated("blackfriday", "sourceRelativeLinksEval",
`There is no replacement for this feature, as no developer has stepped up to the plate and volunteered to maintain this feature`, false)

}

return combinedConfig
}

Expand Down Expand Up @@ -412,8 +401,6 @@ type RenderingContext struct {
DocumentName string
Config *Blackfriday
RenderTOC bool
FileResolver FileResolverFunc
LinkResolver LinkResolverFunc
Cfg config.Provider
}

Expand Down
30 changes: 0 additions & 30 deletions helpers/content_renderer.go
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/gohugoio/hugo/config"
"github.com/miekg/mmark"
"github.com/russross/blackfriday"
jww "github.com/spf13/jwalterweatherman"
)

type LinkResolverFunc func(ref string) (string, error)
Expand All @@ -43,35 +42,6 @@ func (r *HugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string
}
}

func (r *HugoHTMLRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
if r.LinkResolver == nil || bytes.HasPrefix(link, []byte("HAHAHUGOSHORTCODE")) {
// Use the blackfriday built in Link handler
r.Renderer.Link(out, link, title, content)
} else {
// set by SourceRelativeLinksEval
newLink, err := r.LinkResolver(string(link))
if err != nil {
newLink = string(link)
jww.ERROR.Printf("LinkResolver: %s", err)
}
r.Renderer.Link(out, []byte(newLink), title, content)
}
}
func (r *HugoHTMLRenderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
if r.FileResolver == nil || bytes.HasPrefix(link, []byte("HAHAHUGOSHORTCODE")) {
// Use the blackfriday built in Image handler
r.Renderer.Image(out, link, title, alt)
} else {
// set by SourceRelativeLinksEval
newLink, err := r.FileResolver(string(link))
if err != nil {
newLink = string(link)
jww.ERROR.Printf("FileResolver: %s", err)
}
r.Renderer.Image(out, []byte(newLink), title, alt)
}
}

// ListItem adds task list support to the Blackfriday renderer.
func (r *HugoHTMLRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
if !r.Config.TaskLists {
Expand Down
1 change: 0 additions & 1 deletion helpers/content_test.go
Expand Up @@ -188,7 +188,6 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {
ctx.Config.SmartDashes = true
ctx.Config.Smartypants = true
ctx.Config.SmartypantsQuotesNBSP = true
ctx.Config.SourceRelativeLinksEval = true
renderer := c.getHTMLRenderer(defaultFlags, ctx)
actualFlags := renderer.GetFlags()
var expectedFlags int
Expand Down
13 changes: 1 addition & 12 deletions hugolib/page.go
Expand Up @@ -622,22 +622,11 @@ func (p *Page) setAutoSummary() error {
}

func (p *Page) renderContent(content []byte) []byte {
var fn helpers.LinkResolverFunc
var fileFn helpers.FileResolverFunc
if p.getRenderingConfig().SourceRelativeLinksEval {
fn = func(ref string) (string, error) {
return p.Site.SourceRelativeLink(ref, p)
}
fileFn = func(ref string) (string, error) {
return p.Site.SourceRelativeLinkFile(ref, p)
}
}

return p.s.ContentSpec.RenderBytes(&helpers.RenderingContext{
Content: content, RenderTOC: true, PageFmt: p.determineMarkupType(),
Cfg: p.Language(),
DocumentID: p.UniqueID(), DocumentName: p.Path(),
Config: p.getRenderingConfig(), LinkResolver: fn, FileResolver: fileFn})
Config: p.getRenderingConfig()})
}

func (p *Page) getRenderingConfig() *helpers.Blackfriday {
Expand Down
120 changes: 0 additions & 120 deletions hugolib/site.go
Expand Up @@ -493,126 +493,6 @@ func (s *SiteInfo) RelRef(ref string, page *Page, options ...string) (string, er
return s.refLink(ref, page, true, outputFormat)
}

// SourceRelativeLink attempts to convert any source page relative links (like [../another.md]) into absolute links
func (s *SiteInfo) SourceRelativeLink(ref string, currentPage *Page) (string, error) {
var refURL *url.URL
var err error

refURL, err = url.Parse(strings.TrimPrefix(ref, currentPage.getRenderingConfig().SourceRelativeLinksProjectFolder))
if err != nil {
return "", err
}

if refURL.Scheme != "" {
// Not a relative source level path
return ref, nil
}

var target *Page
var link string

if refURL.Path != "" {
refPath := filepath.Clean(filepath.FromSlash(refURL.Path))

if strings.IndexRune(refPath, os.PathSeparator) == 0 { // filepath.IsAbs fails to me.
refPath = refPath[1:]
} else {
if currentPage != nil {
refPath = filepath.Join(currentPage.Source.Dir(), refURL.Path)
}
}

for _, page := range s.AllRegularPages {
if page.Source.Path() == refPath {
target = page
break
}
}
// need to exhaust the test, then try with the others :/
// if the refPath doesn't end in a filename with extension `.md`, then try with `.md` , and then `/index.md`
mdPath := strings.TrimSuffix(refPath, string(os.PathSeparator)) + ".md"
for _, page := range s.AllRegularPages {
if page.Source.Path() == mdPath {
target = page
break
}
}
indexPath := filepath.Join(refPath, "index.md")
for _, page := range s.AllRegularPages {
if page.Source.Path() == indexPath {
target = page
break
}
}

if target == nil {
return "", fmt.Errorf("No page found for \"%s\" on page \"%s\".\n", ref, currentPage.Source.Path())
}

link = target.RelPermalink()

}

if refURL.Fragment != "" {
link = link + "#" + refURL.Fragment

if refURL.Path != "" && target != nil && !target.getRenderingConfig().PlainIDAnchors {
link = link + ":" + target.UniqueID()
} else if currentPage != nil && !currentPage.getRenderingConfig().PlainIDAnchors {
link = link + ":" + currentPage.UniqueID()
}
}

return link, nil
}

// SourceRelativeLinkFile attempts to convert any non-md source relative links (like [../another.gif]) into absolute links
func (s *SiteInfo) SourceRelativeLinkFile(ref string, currentPage *Page) (string, error) {
var refURL *url.URL
var err error

refURL, err = url.Parse(strings.TrimPrefix(ref, currentPage.getRenderingConfig().SourceRelativeLinksProjectFolder))
if err != nil {
return "", err
}

if refURL.Scheme != "" {
// Not a relative source level path
return ref, nil
}

var target *source.File
var link string

if refURL.Path != "" {
refPath := filepath.Clean(filepath.FromSlash(refURL.Path))

if strings.IndexRune(refPath, os.PathSeparator) == 0 { // filepath.IsAbs fails to me.
refPath = refPath[1:]
} else {
if currentPage != nil {
refPath = filepath.Join(currentPage.Source.Dir(), refURL.Path)
}
}

for _, file := range *s.Files {
if file.Path() == refPath {
target = file
break
}
}

if target == nil {
return "", fmt.Errorf("No file found for \"%s\" on page \"%s\".\n", ref, currentPage.Source.Path())
}

link = target.Path()
return "/" + filepath.ToSlash(link), nil
}

return "", fmt.Errorf("failed to find a file to match \"%s\" on page \"%s\"", ref, currentPage.Source.Path())
}

func (s *SiteInfo) addToPaginationPageCount(cnt uint64) {
atomic.AddUint64(&s.paginationPageCount, cnt)
}
Expand Down

0 comments on commit 9891c0f

Please sign in to comment.