Replies: 2 comments
-
Hey there! We generally follow https://ocramius.github.io/blog/when-to-declare-classes-final/ when deciding to make things
True, but even if we removed Have you considered using the decorator pattern? For example, to:
You could wrap/decorate the final class CustomLatexParser implements InlineParserInterface {
private InlineParserInterface $inner;
public function __construct() {
$this->inner = new QuoteParser();
}
public function getMatchDefinition(): InlineParserMatch {
// TODO change this to fit your needs
return InlineParserMatch::oneOf(Quote::SINGLE_QUOTE, Quote::DOUBLE_QUOTE);
}
public function parse(InlineParserContext $inlineContext): bool {
return $this->inner->parse($inlineContext);
}
} Or like this: final class CustomLatexParser implements InlineParserInterface {
public function getMatchDefinition(): InlineParserMatch {
// TODO change this to fit your needs
return InlineParserMatch::oneOf(Quote::SINGLE_QUOTE, Quote::DOUBLE_QUOTE);
}
public function parse(InlineParserContext $inlineContext): bool {
return (new QuoteParser)->parse($inlineContext);
}
} Both are effectively the same as: final class CustomLatexParser extends QuoteParser {
public function getMatchDefinition(): InlineParserMatch {
// TODO change this to fit your needs
return InlineParserMatch::oneOf(Quote::SINGLE_QUOTE, Quote::DOUBLE_QUOTE);
}
} Because you're using one public method from the original class as-is and providing your own implementation for the other. Is that something you'd be open to trying? Or is there some use case that you feel doesn't work well with this approach? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the ideas! That does look like a good way to go. It almost works, but I'm perhaps not thinking about it correctly, but do you think it'd make sense for it to be possible to change the class name that's used in QuoteParser? e.g. the following in League\CommonMark\Extension\SmartPunct\QuoteParser:
Similarly for |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on an extension for converting Markdown to LaTeX, and am trying to integrate the Smart Punctuation extension: samwilson/commonmark-latex#12
The classes in
League\CommonMark\Extension\SmartPunct
are allfinal
and so can't be extended. I don't need to change anything other than the opening and closing quote characters, and the ellipsis rendering. It also needs to prevent SmartPunct from handling dashes.Currently, it looks like the easiest way to do this would be to
LatexRendererExtension
andSmartPunctExtension
from being loaded at the same time; andHowever, this currently means actually duplicating the contents of the
EllipsesParser
,QuoteParser
,QuoteProcessor
, andReplaceUnpairedQuotesListener
classes. If these were not final, then they could just be extended.I'm perhaps missing a better way of doing this though. Does anyone have any ideas?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions