0

I have a directory filled with partials. I'm looking to list ONLY the first h1 tags in each partial. The methods to accomplish this task could probably be modified to grab other elements as well.

Right now I use ruby to open each file, print out the first few characters, close file, and repeat. My ruby file parsing skills are limiting me. Here's the code I have at the moment:

    <% Dir["app/views/partials/show/*.html.erb"].each do |f1| %>
        <% aFile = File.open(f1, "r") %>
        <% if aFile %>
            <% content = aFile.sysread(20) %>
            <p><%= content %></p>
        <% else %>
            <%= "Unable to open file!" %>
        <% end %>
    <% end %>

I also think I'm opening the entire partial in memory? Wondering If I can just read up until I find my h1 tag then close file and move on? Again I'm only reading first 20 characters because I haven't yet grasped a way to search for the first h1 tag.

I'll make edits as I work through the open, parse, piece... I appreciate any guidance and direction you can offer. Thanks!

EDIT: Based on comments below there may be a far better way to accomplish my task. So I'm providing some additional background to get direction on other solutions.

This is for a slide show based on partials in a directory. The slide show is controlled with a navigation element which I would like to populate by the h1 tags in the partials. I'm not going to manually enter these things every time a change is made! I want the end user to simply drag and drop partials into a directory (with a certain name convention and h1 tag description for navigation) and let the slide show do everything else.

I could impose a class on the h1 tag "forNavigation" and on the content "sliderContent" and then use jquery to create a post load <ul> but that doesn't seem right. Plus they'll all be part of the same rendered div.

I guess I'm not clear why reading the first 50 characters of a partial, copying whats in the h1 tags, and putting it in a isn't the most elegant solution?

Like I said, above does everything needed except copy and print whats between the first h1 tag... With an xml parser or some regexp it'll be done. I'm just no good with parsing files.

Please let me know other methods to approach this. Right now I still think it's best to parse the partial (with or without rendering) and put what I need where I want it as needed.

Cœur
  • 37,241
  • 25
  • 195
  • 267
twinturbotom
  • 1,504
  • 1
  • 21
  • 34
  • 1
    Can't see much sense in this, so there may be a far better way to solve your whole problem space. But maybe have a look at [Nokogiri](http://nokogiri.org/), which is a html, xml & sass parser and would most likely do the job in one or two lines of code. – thorsten müller Oct 30 '12 at 15:37
  • Nokogiri doesn't do SASS. It's only HTML and XML. – the Tin Man Oct 30 '12 at 15:46
  • You should move the chunks you want into separate partial files also, rather than try to open and parse them. – the Tin Man Oct 30 '12 at 15:48
  • I'll work with the Nokogiri stuff soon... I'd rather keep everything in one file for ease and consistency...Thanks. – twinturbotom Oct 30 '12 at 20:23
  • nokorigi looks pretty powerful and easy; I'm a little worried about requiring it as I feel like ruby has this functionality. Great info; I appreciate it Thorsten – twinturbotom Oct 30 '12 at 20:37

1 Answers1

0

Partials are not meant to be "parsed", but to be rendered inside other partials and templates. If you need to grab a part of a partial, you should probably extrat that part as a further partial, and use that inner partial in both the "listed" partial and in the "aggregated" view.

rewritten
  • 16,280
  • 2
  • 47
  • 50