Refactor OrgWriter and HTMLWriter: Remove cloning
Extension of the org & html writers is made possible by creating circular references between the extending and extended writer - that way the extending writer can forward all methods it doesn't implement to the extended writer and the extended writer can use the extending writer as the root for method calls to make sure methods overridden in the extending writer are used even for nested method calls. This circular reference leads to problems when cloning writers - cloning the extended writer merely copies the pointer to the extending writer - i.e. the extending writer does not get cloned with an updated reference to the extended writer. Thus method calls to the extending writer act as if no cloning took place and things break. The easiest solution is to just get rid of cloning. We could also clone the ExtendingWriter and replace it's reference to the extended writer with the just cloned one but that's harder so we just remove it. As there are a lot of "extending writer" and "extended writer" in the above paragraphs and I'm too lazy to write up something better here's another attempt at a TLDR: Cloning is broken as ExtendingWriter is a reference to a writer that has a reference to the writer we are cloning - that writer would have to have it's reference updated but that's hard. So we solve it it by not cloning at all.
This commit is contained in:
parent
4292628c80
commit
c9d11e1556
2 changed files with 38 additions and 46 deletions
|
@ -69,16 +69,13 @@ func NewHTMLWriter() *HTMLWriter {
|
|||
}
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) emptyClone() *HTMLWriter {
|
||||
wcopy := *w
|
||||
wcopy.Builder = strings.Builder{}
|
||||
return &wcopy
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) nodesAsString(nodes ...Node) string {
|
||||
tmp := w.emptyClone()
|
||||
WriteNodes(tmp, nodes...)
|
||||
return tmp.String()
|
||||
original := w.Builder
|
||||
w.Builder = strings.Builder{}
|
||||
WriteNodes(w, nodes...)
|
||||
out := w.String()
|
||||
w.Builder = original
|
||||
return out
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) WriterWithExtensions() Writer {
|
||||
|
@ -104,10 +101,12 @@ func (w *HTMLWriter) WritePropertyDrawer(PropertyDrawer) {}
|
|||
func (w *HTMLWriter) WriteBlock(b Block) {
|
||||
content := ""
|
||||
if isRawTextBlock(b.Name) {
|
||||
exportWriter := w.emptyClone()
|
||||
exportWriter.htmlEscape = false
|
||||
WriteNodes(exportWriter, b.Children...)
|
||||
content = strings.TrimRightFunc(exportWriter.String(), unicode.IsSpace)
|
||||
builder, htmlEscape := w.Builder, w.htmlEscape
|
||||
w.Builder, w.htmlEscape = strings.Builder{}, false
|
||||
WriteNodes(w, b.Children...)
|
||||
out := w.String()
|
||||
w.Builder, w.htmlEscape = builder, htmlEscape
|
||||
content = strings.TrimRightFunc(out, unicode.IsSpace)
|
||||
} else {
|
||||
content = w.nodesAsString(b.Children...)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue