From 5dfc1dedb8ac53b7a2d3823d06808ae86f90b3d9 Mon Sep 17 00:00:00 2001
From: spf13 
Date: Thu, 16 Oct 2014 20:20:09 -0400
Subject: [PATCH] Big refactor of how source files are used. Also added default
 destination extension option.
---
 commands/convert.go             |  10 +-
 commands/hugo.go                |   1 +
 create/content.go               |   2 +-
 docs/layouts/partials/menu.html |   2 +-
 helpers/content.go              | 231 ++++++++++++++++++++++++
 helpers/general.go              |  74 ++++----
 helpers/path.go                 |  48 ++++-
 hugolib/page.go                 | 308 +++++++++-----------------------
 hugolib/pageGroup.go            |  13 +-
 hugolib/page_permalink_test.go  |   5 +-
 hugolib/page_test.go            |  10 +-
 hugolib/path_seperators_test.go |   9 +-
 hugolib/permalinks.go           |   9 +-
 hugolib/planner.go              |   2 +-
 hugolib/shortcode.go            |   3 +-
 hugolib/site.go                 |  14 +-
 hugolib/site_show_plan_test.go  |   4 +-
 hugolib/site_test.go            |  57 +++---
 hugolib/site_url_test.go        |   4 +-
 hugolib/summary.go              |  75 --------
 source/file.go                  | 114 ++++++++++++
 source/filesystem.go            |  86 +++++----
 source/filesystem_test.go       |  17 +-
 source/inmemory.go              |  11 +-
 24 files changed, 645 insertions(+), 464 deletions(-)
 create mode 100644 helpers/content.go
 create mode 100644 source/file.go
diff --git a/commands/convert.go b/commands/convert.go
index c9482bea..434c6896 100644
--- a/commands/convert.go
+++ b/commands/convert.go
@@ -99,20 +99,20 @@ func convertContents(mark rune) (err error) {
 
 	jww.FEEDBACK.Println("processing", len(site.Source.Files()), "content files")
 	for _, file := range site.Source.Files() {
-		jww.INFO.Println("Attempting to convert", file.LogicalName)
-		page, err := hugolib.NewPage(file.LogicalName)
+		jww.INFO.Println("Attempting to convert", file.LogicalName())
+		page, err := hugolib.NewPage(file.LogicalName())
 		if err != nil {
 			return err
 		}
 
 		psr, err := parser.ReadFrom(file.Contents)
 		if err != nil {
-			jww.ERROR.Println("Error processing file:", path.Join(file.Dir, file.LogicalName))
+			jww.ERROR.Println("Error processing file:", file.Path())
 			return err
 		}
 		metadata, err := psr.Metadata()
 		if err != nil {
-			jww.ERROR.Println("Error processing file:", path.Join(file.Dir, file.LogicalName))
+			jww.ERROR.Println("Error processing file:", file.Path())
 			return err
 		}
 
@@ -128,7 +128,7 @@ func convertContents(mark rune) (err error) {
 			metadata = newmetadata
 		}
 
-		page.Dir = file.Dir
+		//page.Dir = file.Dir
 		page.SetSourceContent(psr.Content())
 		page.SetSourceMetaData(metadata, mark)
 
diff --git a/commands/hugo.go b/commands/hugo.go
index 1d3bdcd9..7ebd6371 100644
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -118,6 +118,7 @@ func InitializeConfig() {
 	viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
 	viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1})
 	viper.SetDefault("PygmentsStyle", "monokai")
+	viper.SetDefault("DefaultExtension", "html")
 	viper.SetDefault("PygmentsUseClasses", false)
 	viper.SetDefault("DisableLiveReload", false)
 	viper.SetDefault("PluralizeListTitles", true)
diff --git a/create/content.go b/create/content.go
index 449d63b8..bb0f029d 100644
--- a/create/content.go
+++ b/create/content.go
@@ -94,7 +94,7 @@ func NewContent(kind, name string) (err error) {
 		newmetadata["date"] = time.Now().Format(time.RFC3339)
 	}
 
-	page.Dir = viper.GetString("sourceDir")
+	//page.Dir = viper.GetString("sourceDir")
 	page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat")))
 	page.SetSourceContent(psr.Content())
 	if err = page.SafeSaveSourceAs(path.Join(viper.GetString("contentDir"), name)); err != nil {
diff --git a/docs/layouts/partials/menu.html b/docs/layouts/partials/menu.html
index 5a9e1ddf..4c0c53db 100644
--- a/docs/layouts/partials/menu.html
+++ b/docs/layouts/partials/menu.html
@@ -31,7 +31,7 @@
           {{end}}
              Issues & Help 
             {{ if .IsPage }}
-            {{ $File := .File }}  {{with $File.FileName }} Refine this Page {{end}}
+            {{ $File := .File }}  {{with $File.Path }} Refine this Page {{end}}
             {{ end }}
         
         
diff --git a/helpers/content.go b/helpers/content.go
new file mode 100644
index 00000000..802187bc
--- /dev/null
+++ b/helpers/content.go
@@ -0,0 +1,231 @@
+// Copyright © 2014 Steve Francia .
+//
+// Licensed under the Simple Public 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://opensource.org/licenses/Simple-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 helpers
+
+import (
+	"bytes"
+	"html/template"
+	"os/exec"
+
+	"github.com/russross/blackfriday"
+	"github.com/spf13/viper"
+
+	jww "github.com/spf13/jwalterweatherman"
+
+	"strings"
+)
+
+var SummaryLength = 70
+var SummaryDivider = []byte("")
+
+func StripHTML(s string) string {
+	output := ""
+
+	// Shortcut strings with no tags in them
+	if !strings.ContainsAny(s, "<>") {
+		output = s
+	} else {
+		s = strings.Replace(s, "\n", " ", -1)
+		s = strings.Replace(s, "
", " \n", -1)
+		s = strings.Replace(s, "
", " \n", -1)
+		s = strings.Replace(s, "", " \n", -1)
+
+		// Walk through the string removing all tags
+		b := new(bytes.Buffer)
+		inTag := false
+		for _, r := range s {
+			switch r {
+			case '<':
+				inTag = true
+			case '>':
+				inTag = false
+			default:
+				if !inTag {
+					b.WriteRune(r)
+				}
+			}
+		}
+		output = b.String()
+	}
+	return output
+}
+
+func StripEmptyNav(in []byte) []byte {
+	return bytes.Replace(in, []byte("\n\n"), []byte(``), -1)
+}
+
+func BytesToHTML(b []byte) template.HTML {
+	return template.HTML(string(b))
+}
+
+func GetHtmlRenderer(defaultFlags int, footnoteref string) blackfriday.Renderer {
+	renderParameters := blackfriday.HtmlRendererParameters{
+		FootnoteAnchorPrefix:       viper.GetString("FootnoteAnchorPrefix"),
+		FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
+	}
+
+	if len(footnoteref) != 0 {
+		renderParameters.FootnoteAnchorPrefix = footnoteref + ":" +
+			renderParameters.FootnoteAnchorPrefix
+	}
+
+	htmlFlags := defaultFlags
+	htmlFlags |= blackfriday.HTML_USE_XHTML
+	htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
+	htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
+	htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
+	htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS
+
+	return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters)
+}
+
+func GetMarkdownExtensions() int {
+	return 0 | blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
+		blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE |
+		blackfriday.EXTENSION_AUTOLINK | blackfriday.EXTENSION_STRIKETHROUGH |
+		blackfriday.EXTENSION_SPACE_HEADERS | blackfriday.EXTENSION_FOOTNOTES |
+		blackfriday.EXTENSION_HEADER_IDS
+}
+
+func MarkdownRender(content []byte, footnoteref string) []byte {
+	return blackfriday.Markdown(content, GetHtmlRenderer(0, footnoteref),
+		GetMarkdownExtensions())
+}
+
+func MarkdownRenderWithTOC(content []byte, footnoteref string) []byte {
+	return blackfriday.Markdown(content,
+		GetHtmlRenderer(blackfriday.HTML_TOC, footnoteref),
+		GetMarkdownExtensions())
+}
+
+func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
+	origContent := make([]byte, len(content))
+	copy(origContent, content)
+	first := []byte(``)
+
+	replacement := []byte(`