Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ The application uses environment variables to configure all aspects.
* `J_DB_PATH` - Path to SQLite DB - default is `$GOPATH/data/journal.db`
* `J_DESCRIPTION` - Set the HTML description of the Journal
* `J_EDIT` - Set to `0` to disable article modification
* `J_EXCERPT_WORDS` - The length of the article shown as a preview/excerpt in the index, default `50`
* `J_GA_CODE` - Google Analytics tag value, starts with `UA-`, or ignore to disable Google Analytics
* `J_PORT` - Port to expose over HTTP, default is `3000`
* `J_THEME` - Theme to use from within the _web/themes_ folder, defaults to `default`
Expand Down
6 changes: 6 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Configuration struct {
Description string
EnableCreate bool
EnableEdit bool
ExcerptWords int
GoogleAnalyticsCode string
Port string
SSLCertificate string
Expand All @@ -55,6 +56,7 @@ func DefaultConfiguration() Configuration {
Description: "A private journal containing Jamie's innermost thoughts",
EnableCreate: true,
EnableEdit: true,
ExcerptWords: 50,
GoogleAnalyticsCode: "",
Port: "3000",
SSLCertificate: "",
Expand Down Expand Up @@ -88,6 +90,10 @@ func ApplyEnvConfiguration(config *Configuration) {
if enableEdit == "0" {
config.EnableEdit = false
}
excerptWords, _ := strconv.Atoi(os.Getenv("J_EXCERPT_WORDS"))
if excerptWords > 0 {
config.ExcerptWords = excerptWords
}
config.GoogleAnalyticsCode = os.Getenv("J_GA_CODE")
port := os.Getenv("J_PORT")
if port != "" {
Expand Down
5 changes: 5 additions & 0 deletions internal/app/controller/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Index struct {

type indexTemplateData struct {
Container interface{}
Excerpt func(model.Journal) string
Journals []model.Journal
Pages []int
Pagination database.PaginationDisplay
Expand Down Expand Up @@ -49,6 +50,10 @@ func (c *Index) Run(response http.ResponseWriter, request *http.Request) {
i++
}

data.Excerpt = func(j model.Journal) string {
return j.GetHTMLExcerpt(container.Configuration.ExcerptWords)
}

c.SaveSession(response)
template, _ := template.ParseFiles(
"./web/templates/_layout/default.html.tmpl",
Expand Down
25 changes: 3 additions & 22 deletions internal/app/model/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,8 @@ func (j Journal) GetEditableDate() string {
return re.FindString(j.Date)
}

// GetExcerpt returns a small extract of the entry as plain text
func (j Journal) GetExcerpt() string {
strip := regexp.MustCompile("\b+")
// Markdown handling - replace newlines with spaces
text := strings.ReplaceAll(j.Content, "\n", " ")
text = strip.ReplaceAllString(text, " ")

// Clean up multiple spaces
spaceRegex := regexp.MustCompile(`\s+`)
text = spaceRegex.ReplaceAllString(text, " ")

words := strings.Split(text, " ")

if len(words) > 50 {
return strings.Join(words[:50], " ") + "..."
}
return strings.TrimSpace(strings.Join(words, " "))
}

// GetHTMLExcerpt returns a small extract of the entry rendered as HTML
func (j Journal) GetHTMLExcerpt() string {
func (j Journal) GetHTMLExcerpt(maxWords int) string {
if j.Content == "" {
return ""
}
Expand All @@ -86,7 +67,7 @@ func (j Journal) GetHTMLExcerpt() string {

for _, paragraph := range paragraphs {
// Skip if we've already got 50+ words
if wordCount >= 50 {
if wordCount >= maxWords {
break
}

Expand All @@ -98,7 +79,7 @@ func (j Journal) GetHTMLExcerpt() string {
lineWords := strings.Fields(line)

// Calculate how many words we can take from this line
wordsToTake := 50 - wordCount
wordsToTake := maxWords - wordCount
if wordsToTake <= 0 {
break
}
Expand Down
24 changes: 12 additions & 12 deletions internal/app/model/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,42 @@ func TestJournal_GetEditableDate(t *testing.T) {
}
}

func TestJournal_GetExcerpt(t *testing.T) {
func TestJournal_GetHTMLExcerpt(t *testing.T) {
tables := []struct {
input string
output string
}{
{"Some simple text", "Some simple text"},
{"Multiple\n\nparagraphs, some with\n\nmultiple words", "Multiple paragraphs, some with multiple words"},
{"Some **bold** text", "<p>Some <strong>bold</strong> text</p>\n"},
{"Multiple\n\nparagraphs", "<p>Multiple</p>\n\n<p>paragraphs</p>\n"},
{"", ""},
{"\n\n", ""},
{"a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z", "a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x..."},
{"*Italic* and **bold**", "<p><em>Italic</em> and <strong>bold</strong></p>\n"},
{"Line 1\nLine 2\nLine 3", "<p>Line 1\nLine 2\nLine 3</p>\n"},
}

for _, table := range tables {
j := Journal{Content: table.input}
actual := j.GetExcerpt()
actual := j.GetHTMLExcerpt(50)
if actual != table.output {
t.Errorf("Expected GetExcerpt() to produce result of '%s', got '%s'", table.output, actual)
t.Errorf("Expected GetHTMLExcerpt() to produce result of '%s', got '%s'", table.output, actual)
}
}
}

func TestJournal_GetHTMLExcerpt(t *testing.T) {
func TestJournal_GetHTMLExcerpt_ShortWords(t *testing.T) {
tables := []struct {
input string
output string
}{
{"Some **bold** text", "<p>Some <strong>bold</strong> text</p>\n"},
{"Some **bold** text", "<p>Some <strong>bold</strong>&hellip;</p>\n"},
{"Multiple\n\nparagraphs", "<p>Multiple</p>\n\n<p>paragraphs</p>\n"},
{"", ""},
{"*Italic* and **bold**", "<p><em>Italic</em> and <strong>bold</strong></p>\n"},
{"Line 1\nLine 2\nLine 3", "<p>Line 1\nLine 2\nLine 3</p>\n"},
{"*Italic* and **bold**", "<p><em>Italic</em> and&hellip;</p>\n"},
{"Line 1\nLine 2\nLine 3", "<p>Line 1</p>\n"},
}

for _, table := range tables {
j := Journal{Content: table.input}
actual := j.GetHTMLExcerpt()
actual := j.GetHTMLExcerpt(2)
if actual != table.output {
t.Errorf("Expected GetHTMLExcerpt() to produce result of '%s', got '%s'", table.output, actual)
}
Expand Down
2 changes: 1 addition & 1 deletion web/templates/index.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{.GetDate}}
</p>
<div class="summary">
{{.GetHTMLExcerpt}}
{{call $.Excerpt . }}
{{if $enableEdit}}<span class="float-right"><a href="/{{.Slug}}/edit" class="button button-outline">Edit</a></span>{{end}}
<p><a href="/{{.Slug}}">Read More</a></p>
</div>
Expand Down