Currently, I want to use the library qiita-markdown to display the source code on my blog, but I didn't like the output, so I tried to fiddle with the source code inside, and I asked because it was written in a somewhat redundant way.
The following are some of the things that bothered me.
class Highlighter
default initialize (default_language:nil, node:nil, specific_language:nil)
@default_language=default_language
@node=node
@specific_language=specific_language
end
def self.call(*args)
new(*args).call
end
def call
outer=Nokogiri::HTML.fragment(%Q[<div class="code-frame"data-lang="#{language}">])
frame=outer.at("div")
frame.add_child(filename_node) if filename
frame.add_child (highlighted_node)
@node.replace(outer)
end
### It was used in the source code as follows.
Highlighter.call(
default_language —default_language,
node —node,
specific_language —timeout_fallback_language,
)
Why bother calling the call, the method of the Highlighter class, to generate instances of the Highlighter class?
Normally, I think the writing style is as follows.
class highlighter
default initialize (default_language:nil, node:nil, specific_language:nil)
@default_language=default_language
@node=node
@specific_language=specific_language
end
def call
outer=Nokogiri::HTML.fragment(%Q[<div class="code-frame"data-lang="#{language}">])
frame=outer.at("div")
frame.add_child(filename_node) if filename
frame.add_child (highlighted_node)
@node.replace(outer)
end
Highlighter.new(
default_language —default_language,
node —node,
specific_language —timeout_fallback_language,
) .call
I think there's a reason for this kind of redundant writing.
I'd appreciate it if you could tell me why.
Maybe it's simply because creating a Highlight object every time is laborious. If you call after new, and if you call Highlighter (if you call), it will be highlighted and returned, I think the latter one will take some less time.
© 2024 OneMinuteCode. All rights reserved.