Fancy Filter?

15 views
Skip to first unread message

Pito Salas

unread,
Feb 26, 2021, 9:56:26 PM2/26/21
to na...@googlegroups.com
Hi there Denis,

Here's what I've come up with. It took a bit of struggle because until late I didn't realize that content wasn't a simple variable. But this seems pretty ugly! Could you give me an idea on how to clean it up? THanks,

Pito


class CensorFilter < Nanoc::Filter
identifier :handle_shortcodes

def run(content, params = {})
c = content
if c =~ /\$\$INCLUDE_TOPIC :/
c = c.gsub(/^\$\$INCLUDE_TOPIC :(\w+)$/) {"<%= include_topic :#{$1} %>" }
end
if c =~ /\$\$SLIDE_TITLE .+$/
c = c.gsub(/^\$\$SLIDE_TITLE (.+)$/) {"<slide_break></slide_break>\n\n\### #{$1}"}
end
if c =~ /\$\$NEW_SLIDE$/
c = c.gsub(/^\$\$NEW_SLIDE$/) {"<slide_break></slide_break>"}
end
c
end
end

Trejkaz

unread,
Mar 3, 2021, 8:57:59 PM3/3/21
to na...@googlegroups.com
I’d remove the `if` blocks and just run the `gsub` calls. Having both there means you’re doubling up the same (or almost the same) regex twice in a row, and a gsub is going to be a no-op if it doesn’t match anyway.

Then your assignments to `c` become redundant as well and you can just chain the `gsub` calls.

TX


--
You received this message because you are subscribed to the Google Groups "nanoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanoc+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nanoc/8E962522-DE7D-410E-8CDE-8055BBFD153E%40gmail.com.
Reply all
Reply to author
Forward
0 new messages