From 512ba73f10e8ea2b6fb857f58c169bbff7b61f05 Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Tue, 17 Nov 2015 23:43:24 +0200 Subject: [PATCH] Add feature to hide "Read more" if there is no more content to read --- config/config.exs.dist | 3 +++ lib/mebe_engine/models.ex | 3 ++- lib/mebe_engine/parser.ex | 7 +++++-- web/controllers/controller_utils.ex | 2 ++ web/templates/page/post_elem.html.eex | 14 ++++++++------ 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/config/config.exs.dist b/config/config.exs.dist index aee05c5..6589ee9 100644 --- a/config/config.exs.dist +++ b/config/config.exs.dist @@ -22,6 +22,9 @@ config :mebe_web,, blog_name: "My awesome blog", blog_author: "Author McAuthor", absolute_url: "http://localhost:4000", # Absolute URL to the site without trailing slash, including protocol + + force_read_more: false, # Force "Read more…" text to display even if there is no more content + enable_feeds: false, # Set to true to enable RSS feeds feeds_full_content: false, # Show full content in feeds instead of short content diff --git a/lib/mebe_engine/models.ex b/lib/mebe_engine/models.ex index e6406b9..bc0ba52 100644 --- a/lib/mebe_engine/models.ex +++ b/lib/mebe_engine/models.ex @@ -17,7 +17,8 @@ defmodule MebeEngine.Models do tags: [], content: nil, short_content: nil, - order: 0 + order: 0, + has_more: false end defmodule Page do diff --git a/lib/mebe_engine/parser.ex b/lib/mebe_engine/parser.ex index d1433ce..200875b 100644 --- a/lib/mebe_engine/parser.ex +++ b/lib/mebe_engine/parser.ex @@ -67,14 +67,17 @@ defmodule MebeEngine.Parser do order = format_order order + split_content = String.split content, ~R//u + %Post{ slug: slug, title: title, date: date_to_int_tuple({year, month, day}), tags: parse_tags(tags), content: content, - short_content: hd(String.split content, ~R//u), - order: order + short_content: hd(split_content), + order: order, + has_more: (Enum.count(split_content) > 1) } end end diff --git a/web/controllers/controller_utils.ex b/web/controllers/controller_utils.ex index 876873f..405e6ba 100644 --- a/web/controllers/controller_utils.ex +++ b/web/controllers/controller_utils.ex @@ -13,6 +13,7 @@ defmodule MebeWeb.ControllerUtils do @page_commenting Application.get_env(:mebe_web, :page_commenting) @disqus_shortname Application.get_env(:mebe_web, :disqus_shortname) @feeds_enabled Application.get_env(:mebe_web, :enable_feeds) + @force_read_more Application.get_env(:mebe_web, :force_read_more) @doc """ Render a list of posts with the given template and params. The posts @@ -38,5 +39,6 @@ defmodule MebeWeb.ControllerUtils do |> assign(:page_commenting, @page_commenting) |> assign(:disqus_shortname, @disqus_shortname) |> assign(:feeds_enabled, @feeds_enabled) + |> assign(:force_read_more, @force_read_more) end end diff --git a/web/templates/page/post_elem.html.eex b/web/templates/page/post_elem.html.eex index 38071bf..939ad62 100644 --- a/web/templates/page/post_elem.html.eex +++ b/web/templates/page/post_elem.html.eex @@ -43,11 +43,13 @@
<%= raw @post.short_content %> -

- - Read more… - -

+ <%= if @force_read_more or @post.has_more do %> +

+ + Read more… + +

+ <% end %>
<%= else %>
@@ -70,7 +72,7 @@ })(); <% end %>