Repository: getnikola/nikola

Issue:
Add support for microblog-style posts without titles
Like Dave Winer and others, I like to have some posts without titles. This is easy enough to implement with the templates. But there are a number of different conditions that can cause the title to be absent:

* Use of the `hidetitle` metadata
* Title is the "NO TITLE" placeholder
* Title is literally blank
* Post type: I'm using "micro" to indicate a microblog post.

As I say, I've got this working reasonably well _in my fork of one particular theme_. But my templates need to have various duplicated `if` conditions scattered through them, and anyone wanting similar functionality doesn't benefit. I would be good if there were a built-in feature by which templates could identify posts for which they should not render the title.

I propose adding a function to `Post.py`, such as `should_show_title()`. It would check the first three of the points above. For the fourth, I'm thinking of a new config item, something like `TYPES_WITHOUT_TITLES`, which would contain an optional list of of post types for which titles are not to be shown.

I plan to start work on this, but I'm open to suggestions for alternative approaches, corrections to my thinking, or what have you. I'm new to the Nikola codebase, so I'll be learning it as I go.



Discussion:
What slugs do you use for your micro posts? How do you identify them if they don’t have a title?
> What slugs do you use for your micro posts? How do you identify them if they don’t have a title?

That's a good question. I imported my site from WordPress, so most of them have the WP post ID number as the slug. Since starting to use Nikola, I haven't quite settled on what to use. I've tried using the date in ISO format (2020-05-06), but that obviously gets problematic if I post more than once in a day. And it duplicates information that's already in the URL path.

For some I have included a title (which generates the stub), but using the `micro` post type, my template suppresses it. That's the easiest way, since `nikola new_post` needs a title.

I'd quite like it if there were a `nikola micro` option that didn't need a title, and that used something like the WordPress ID. But that's probably not worth the effort.



Doing a command that does the same as new_post but doesn't ask for a title
is very easy.

You can make it a plugin, then anyone can use it.

On Wed, May 6, 2020 at 8:28 AM Martin McCallion <notifications@github.com>
wrote:

> What slugs do you use for your micro posts? How do you identify them if
> they don’t have a title?
>
> That's a good question. I imported my site from WordPress, so most of them
> have the WP post ID number as the slug. Since starting to use Nikola, I
> haven't quite settled on what to use. I've tried using the date in ISO
> format (2020-05-06), but that obviously gets problematic if I post more
> than once in a day. And it duplicates information that's already in the URL
> path.
>
> For some I have included a title, but using the micro post type, my
> template suppresses it. That's the easiest way, since nikola new_post
> needs a title.
>
> I'd quite like it if there were a nikola micro option that didn't need a
> title, and that used something like the WordPress ID. But that's probably
> not worth the effort.
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/getnikola/nikola/issues/3408#issuecomment-624594115>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAAAMKYO2AQC7STCGEO7X73RQFCTXANCNFSM4M2LHSWA>
> .
>

You can even inherit CommandNewPost :-)

On Wed, May 6, 2020 at 8:33 AM Roberto Alsina <ralsina@kde.org> wrote:

> Doing a command that does the same as new_post but doesn't ask for a title
> is very easy.
>
> You can make it a plugin, then anyone can use it.
>
> On Wed, May 6, 2020 at 8:28 AM Martin McCallion <notifications@github.com>
> wrote:
>
>> What slugs do you use for your micro posts? How do you identify them if
>> they don’t have a title?
>>
>> That's a good question. I imported my site from WordPress, so most of
>> them have the WP post ID number as the slug. Since starting to use Nikola,
>> I haven't quite settled on what to use. I've tried using the date in ISO
>> format (2020-05-06), but that obviously gets problematic if I post more
>> than once in a day. And it duplicates information that's already in the URL
>> path.
>>
>> For some I have included a title, but using the micro post type, my
>> template suppresses it. That's the easiest way, since nikola new_post
>> needs a title.
>>
>> I'd quite like it if there were a nikola micro option that didn't need a
>> title, and that used something like the WordPress ID. But that's probably
>> not worth the effort.
>>
>> —
>> You are receiving this because you are subscribed to this thread.
>> Reply to this email directly, view it on GitHub
>> <https://github.com/getnikola/nikola/issues/3408#issuecomment-624594115>,
>> or unsubscribe
>> <https://github.com/notifications/unsubscribe-auth/AAAAMKYO2AQC7STCGEO7X73RQFCTXANCNFSM4M2LHSWA>
>> .
>>
>

> Doing a command that does the same as new_post but doesn't ask for a title is very easy. You can make it a plugin, then anyone can use it.

I'll give that some thought. My "probably not worth the effort" thought was more about the idea of generating suitable slugs in the style of the WordPress ID number. It's something you could do, but it could easily get complex.

On the other hand, a simple file containing a number, read, updated, written back... 
On Wed, May 6, 2020 at 8:44 AM Martin McCallion <notifications@github.com>
wrote:

> Doing a command that does the same as new_post but doesn't ask for a title
> is very easy. You can make it a plugin, then anyone can use it.
>
> I'll give that some thought. My "probably not worth the effort" thought
> was more about the idea of generating suitable slugs in the style of the
> WordPress ID number. It's something you could do, but it could easily get
> complex.
>
> On the other hand, a simple file containing a number, read, updated,
> written back...
>

You can put that number in "state" (self.site.state in any plugin)  but if
that ever gets lost then things will get messed up.
If all you need is this to be reasonably unique, you can use something like
str(uuid.uuid4())[:8] ... the odds of collision are tiny.



> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/getnikola/nikola/issues/3408#issuecomment-624601055>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAAAMK3R5G2LBZA5QDNLA6LRQFERRANCNFSM4M2LHSWA>
> .
>


Resolving patch:
diff --git a/nikola/nikola.py b/nikola/nikola.py
--- a/nikola/nikola.py
+++ b/nikola/nikola.py
@@ -619,6 +619,7 @@ def __init__(self, **config):
             'GITHUB_COMMIT_SOURCE': False,  # WARNING: conf.py.in overrides this with True for backwards compatibility
             'META_GENERATOR_TAG': True,
             'REST_FILE_INSERTION_ENABLED': True,
+            'TYPES_TO_HIDE_TITLE': [],
         }
 
         # set global_context for template rendering
@@ -1793,7 +1794,7 @@ def generic_rss_feed(self, lang, title, link, description, timeline,
                         else:  # let other errors raise
                             raise
             args = {
-                'title': post.title(lang),
+                'title': post.title(lang) if post.should_show_title() else None,
                 'link': post.permalink(lang, absolute=True, query=feed_append_query),
                 'description': data,
                 # PyRSS2Gen's pubDate is GMT time.
diff --git a/nikola/post.py b/nikola/post.py
--- a/nikola/post.py
+++ b/nikola/post.py
@@ -177,6 +177,7 @@ def _load_config(self, config):
         self.translations = self.config['TRANSLATIONS']
         self.skip_untranslated = not self.config['SHOW_UNTRANSLATED_POSTS']
         self._default_preview_image = self.config['DEFAULT_PREVIEW_IMAGE']
+        self.types_to_hide_title = self.config['TYPES_TO_HIDE_TITLE']
 
     def _set_tags(self):
         """Set post tags."""
@@ -1089,6 +1090,15 @@ def source_ext(self, prefix=False):
         else:
             return ext
 
+    def should_hide_title(self):
+        """Return True if this post's title should be hidden. Use in templates to manage posts without titles."""
+        return self.title().strip() in ('NO TITLE', '') or self.meta('hidetitle') or \
+            self.meta('type').strip() in self.types_to_hide_title
+
+    def should_show_title(self):
+        """Return True if this post's title should be displayed. Use in templates to manage posts without titles."""
+        return not self.should_hide_title()
+
 
 def get_metadata_from_file(source_path, post, config, lang, metadata_extractors_by):
     """Extract metadata from the file itself, by parsing contents."""


Test patch: