wojtekmach / easyhtml

EasyHTML makes it easy to work with HTML in Elixir.
45 stars 3 forks source link

Implement Enumerable for EasyHTML #3

Closed kwando closed 7 months ago

kwando commented 7 months ago

I think it would be nice to implement the Enumerable for EasyHTML.

So support for code like this :)


for article <- doc["article"] do
  to_string(article[".title"])
end
wojtekmach commented 7 months ago

Yeah this would be very nice. A PR would be appreciated.

@philss can you think of any disadvantages of doing this?

kwando commented 7 months ago

I gave it a shot but had some trouble getting it to work.

Will give it a shot again :)

philss commented 7 months ago

@philss can you think of any disadvantages of doing this?

No, I like the idea and I don't see a problem right now! I think this would be implemented slightly different if we implement your proposal to Floki. But I think it would work the same :)

kwando commented 7 months ago

This would be nice too.. but not sure how doable that is since "Enum." just turns lists.

for row <- doc["tr"] do
  for cell <- row["td"] do
    to_string(cell)
  end
end

Edit: changed the example

wojtekmach commented 7 months ago

I believe the trick is for the enumerable implementation to emit elements as structs so you can enumerate them and call x[y] on them.

wojtekmach commented 7 months ago

Cloning in favour of https://github.com/wojtekmach/easyhtml/pull/4.