Pito Salas wrote:
> Nanoc::Filter.define(:handle_shortcodes) do |content, _params = {}|
> result = ""
> if content =~ /\$\$INCLUDE_TOPIC/
> result = content.gsub(/^\$\$INCLUDE_TOPIC :(\w+)$/) { "<%= include_topic(#{$1}) %>" }
> elsif content =~ /\$\$NEWSLIDE/
> puts "1"
> result = content.gsub(/^\$\$NEWSLIDE (\w+)$/) { "<slide_break></slide_break>\n\n####{$1}" }
That will replace $$INCLUDE_TOPIC if it is present in your content,
but will replace $$NEWSLIDE only if $$INCLUDE_TOPIC is _not_ present.
If you have both in your source file, your code will find
$$INCLUDE_TOPIC and do the replacement and then exit. If you want to
check for and replace both, "elsif" is the wrong way.
> It took a bit of struggle because until late I didn't realize that content wasn't a simple variable.
It is a "simple" variable containing all the content of your source
file.
> Could you give me an idea on how to clean it up?
You want to apply a list of substitutions to the content, i.e.
repeatedly do the same with different content; that's what a loop
does. So you could use a hash with the shorthand (i.e.
"$$INCLUDE_TOPIC") as key and the substitution as value and iterate
over it.
-thh