gma / nesta

File Based CMS and Static Site Generator
http://nestacms.com
MIT License
902 stars 122 forks source link

Parse heading with appropriate parser after extracting it #84

Closed gryzzly closed 12 years ago

gryzzly commented 12 years ago

If I have page with the following content:

Date: 19 December 2011

# JavaScript inheritance and `prototype` object

Here goes content …

The page.heading should return "JavaScript inheritance and <code>prototype</code> object". So that it will align with actual contents of your h1 on the page. There is page.title that is supposed to hold plain text version of the page's title.

I've tried to mess with the following code, but markup is a private property, so I can't override it in app.rb.

class Page < FileModel
  def heading
    regex = case @format
      when :mdown
        /^#\s*(.*?)(\s*#+|$)/
      when :haml
        /^\s*%h1\s+(.*)/
      when :textile
        /^\s*h1\.\s+(.*)/
      end
    markup =~ regex
    Regexp.last_match(1)
  end
end

Thanks!

gma commented 12 years ago

Thanks for the report. How big a problem is it causing you at the moment? I just had a quick scan of the code and it looks as though h1 tags are rendered using the appropriate rendering engine on the page, but that the Page#heading method returns them clean, without any modification (which is also what you're implying).

Did you mean that you "can't override it in app.rb"? If not, why not?

I'm wondering if there are some scenarios in which it would make sense for Page#heading to return a clean unmarked up version of the heading.

gryzzly commented 12 years ago

Hey Graham, thanks for your fast reply.

If I try to modify heading, I need markup and @format to be available for me, what I get is NoMethodError at ... private method 'format' called for #<Nesta::Page:0x00000....>.

The reason I need this is that in summaries.haml partial I print headings and modified body separately, so when I do

%h1
  %a(href="#{page.abspath}")= page.heading
%div
  = html_truncate( page.body, 30, ' [...]' )

I want the resulting %h1 to match the one that will be rendered on the page itself. So I want to have unified output for both. And since there is "page.title" for plain text, as an alternative, I think it should actually work like it does on the page itself (page.heading should be parsed).

Thanks!

gma commented 12 years ago

Ah, I see what you mean with the scoping. There are ways around that but I haven't time to play just now to generate a working snippet for you. If you want to hack it, you can call a private method in Ruby with send (e.g. send(:format)). That is a hack though; I wouldn't recommend it under normal circumstances.

Unfortunately title isn't actually a plain text version of heading. There are good reasons to want to keep them separate, and you can actually override the page title by specifying "Title: ..." metadata at the top of your page.

In your code snippet, pass self to the call to body (see https://github.com/gma/nesta/blob/master/views/summaries.haml). If you don't then (should you ever decide to use it) articles written in .haml files won't have access to the Ruby helper methods (which is often the point of using Haml).

I need to look at where Page#heading is already being used to see whether or not converting it with a rendering engine is a safe thing to do. If not, we need a new method.

What I'd do if I was you (rather than overriding the heading method) is to make a new method called marked_up_heading. In it I'd call heading to get the raw text, and then convert_to_html in order to get the HTML. Then call that in your template…

gryzzly commented 12 years ago

Hey, a quick note, I did really mean the title from metadata when I was referring to title, my point was that if somebody wants the plain text version of a "title" or "heading" whatever you call it, so he may have it in title: metadata or parsed out of the first heading.

gryzzly commented 12 years ago

What I'd do if I was you (rather than overriding the heading method) is to make a new method called marked_up_heading. In it I'd call heading to get the raw text, and then convert_to_html in order to get the HTML. Then call that in your template…

But in order to properly "convert" it, I need the format again, right? So probably for now I'll have to use hacky send?

Thanks!

gma commented 12 years ago

I see. I just meant that the title and heading will often have different words in them, so you can't use one method in place of the other.

gma commented 12 years ago

But in order to properly "convert" it, I need the format again, right? So probably for now I'll have to use hacky send?

Yeah, I would. Perhaps it shouldn't be private…

gma commented 12 years ago

I'm closing this; it's a duplicate of #78.