From 9c9003363f693a5c06f6867e617697afb1447405 Mon Sep 17 00:00:00 2001 From: yuin Date: Sat, 28 Oct 2023 17:57:55 +0900 Subject: [PATCH] Simplify EastAsianLineBreaks --- .golangci.yml | 2 +- extension/cjk.go | 47 ++++++++++--------------- renderer/html/html.go | 79 +++++++++++++++---------------------------- 3 files changed, 46 insertions(+), 82 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 28d8b9c..39fe61d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -45,7 +45,7 @@ linters-settings: disabled: false - name: dot-imports severity: warning - disabled: false + disabled: true - name: error-return severity: warning disabled: false diff --git a/extension/cjk.go b/extension/cjk.go index aabafad..a3238c2 100644 --- a/extension/cjk.go +++ b/extension/cjk.go @@ -9,13 +9,15 @@ import ( // A CJKOption sets options for CJK support mostly for HTML based renderers. type CJKOption func(*cjk) -// A EastAsianLineBreaksStyle is a style of east asian line breaks. -type EastAsianLineBreaksStyle int +// A EastAsianLineBreaks is a style of east asian line breaks. +type EastAsianLineBreaks int const ( - // EastAsianLineBreaksStyleSimple is a style where soft line breaks are ignored + //EastAsianLineBreaksNone renders line breaks as it is. + EastAsianLineBreaksNone EastAsianLineBreaks = iota + // EastAsianLineBreaksSimple is a style where soft line breaks are ignored // if both sides of the break are east asian wide characters. - EastAsianLineBreaksStyleSimple EastAsianLineBreaksStyle = iota + EastAsianLineBreaksSimple // EastAsianLineBreaksCSS3Draft is a style where soft line breaks are ignored // even if only one side of the break is an east asian wide character. EastAsianLineBreaksCSS3Draft @@ -23,16 +25,14 @@ const ( // WithEastAsianLineBreaks is a functional option that indicates whether softline breaks // between east asian wide characters should be ignored. -func WithEastAsianLineBreaks(style ...EastAsianLineBreaksStyle) CJKOption { +// style defauts to [EastAsianLineBreaksSimple] . +func WithEastAsianLineBreaks(style ...EastAsianLineBreaks) CJKOption { return func(c *cjk) { - e := &eastAsianLineBreaks{ - Enabled: true, - EastAsianLineBreaksStyle: EastAsianLineBreaksStyleSimple, + if len(style) == 0 { + c.EastAsianLineBreaks = EastAsianLineBreaksSimple + return } - for _, s := range style { - e.EastAsianLineBreaksStyle = s - } - c.EastAsianLineBreaks = e + c.EastAsianLineBreaks = style[0] } } @@ -44,21 +44,18 @@ func WithEscapedSpace() CJKOption { } type cjk struct { - EastAsianLineBreaks *eastAsianLineBreaks + EastAsianLineBreaks EastAsianLineBreaks EscapedSpace bool } -type eastAsianLineBreaks struct { - Enabled bool - EastAsianLineBreaksStyle EastAsianLineBreaksStyle -} - // CJK is a goldmark extension that provides functionalities for CJK languages. var CJK = NewCJK(WithEastAsianLineBreaks(), WithEscapedSpace()) // NewCJK returns a new extension with given options. func NewCJK(opts ...CJKOption) goldmark.Extender { - e := &cjk{} + e := &cjk{ + EastAsianLineBreaks: EastAsianLineBreaksNone, + } for _, opt := range opts { opt(e) } @@ -66,16 +63,8 @@ func NewCJK(opts ...CJKOption) goldmark.Extender { } func (e *cjk) Extend(m goldmark.Markdown) { - if e.EastAsianLineBreaks != nil { - if e.EastAsianLineBreaks.Enabled { - style := html.EastAsianLineBreaksStyleSimple - switch e.EastAsianLineBreaks.EastAsianLineBreaksStyle { - case EastAsianLineBreaksCSS3Draft: - style = html.EastAsianLineBreaksCSS3Draft - } - m.Renderer().AddOptions(html.WithEastAsianLineBreaks(style)) - } - } + m.Renderer().AddOptions(html.WithEastAsianLineBreaks( + html.EastAsianLineBreaks(e.EastAsianLineBreaks))) if e.EscapedSpace { m.Renderer().AddOptions(html.WithWriter(html.NewWriter(html.WithEscapedSpace()))) m.Parser().AddOptions(parser.WithEscapedSpace()) diff --git a/renderer/html/html.go b/renderer/html/html.go index 2c48f3c..8738c2a 100644 --- a/renderer/html/html.go +++ b/renderer/html/html.go @@ -17,7 +17,7 @@ import ( type Config struct { Writer Writer HardWraps bool - EastAsianLineBreaks eastAsianLineBreaks + EastAsianLineBreaks EastAsianLineBreaks XHTML bool Unsafe bool } @@ -27,7 +27,7 @@ func NewConfig() Config { return Config{ Writer: DefaultWriter, HardWraps: false, - EastAsianLineBreaks: eastAsianLineBreaks{}, + EastAsianLineBreaks: EastAsianLineBreaksNone, XHTML: false, Unsafe: false, } @@ -39,7 +39,7 @@ func (c *Config) SetOption(name renderer.OptionName, value interface{}) { case optHardWraps: c.HardWraps = value.(bool) case optEastAsianLineBreaks: - c.EastAsianLineBreaks = value.(eastAsianLineBreaks) + c.EastAsianLineBreaks = value.(EastAsianLineBreaks) case optXHTML: c.XHTML = value.(bool) case optUnsafe: @@ -104,29 +104,31 @@ func WithHardWraps() interface { // EastAsianLineBreaks is an option name used in WithEastAsianLineBreaks. const optEastAsianLineBreaks renderer.OptionName = "EastAsianLineBreaks" -// A EastAsianLineBreaksStyle is a style of east asian line breaks. -type EastAsianLineBreaksStyle int +// A EastAsianLineBreaks is a style of east asian line breaks. +type EastAsianLineBreaks int const ( - // EastAsianLineBreaksStyleSimple follows east_asian_line_breaks in Pandoc. - EastAsianLineBreaksStyleSimple EastAsianLineBreaksStyle = iota + //EastAsianLineBreaksNone renders line breaks as it is. + EastAsianLineBreaksNone EastAsianLineBreaks = iota + // EastAsianLineBreaksSimple follows east_asian_line_breaks in Pandoc. + EastAsianLineBreaksSimple // EastAsianLineBreaksCSS3Draft follows CSS text level3 "Segment Break Transformation Rules" with some enhancements. EastAsianLineBreaksCSS3Draft ) -type eastAsianLineBreaker interface { - SoftLineBreak(thisLastRune rune, siblingFirstRune rune) bool -} - -type eastAsianLineBreaksSimple struct{} - -func (e *eastAsianLineBreaksSimple) SoftLineBreak(thisLastRune rune, siblingFirstRune rune) bool { - return !(util.IsEastAsianWideRune(thisLastRune) && util.IsEastAsianWideRune(siblingFirstRune)) +func (b EastAsianLineBreaks) softLineBreak(thisLastRune rune, siblingFirstRune rune) bool { + switch b { + case EastAsianLineBreaksNone: + return false + case EastAsianLineBreaksSimple: + return !(util.IsEastAsianWideRune(thisLastRune) && util.IsEastAsianWideRune(siblingFirstRune)) + case EastAsianLineBreaksCSS3Draft: + return eastAsianLineBreaksCSS3DraftSoftLineBreak(thisLastRune, siblingFirstRune) + } + return false } -type eastAsianLineBreaksCSS3Draft struct{} - -func (e *eastAsianLineBreaksCSS3Draft) SoftLineBreak(thisLastRune rune, siblingFirstRune rune) bool { +func eastAsianLineBreaksCSS3DraftSoftLineBreak(thisLastRune rune, siblingFirstRune rune) bool { // Implements CSS text level3 Segment Break Transformation Rules with some enhancements. // References: // - https://www.w3.org/TR/2020/WD-css-text-3-20200429/#line-break-transform @@ -171,52 +173,25 @@ func (e *eastAsianLineBreaksCSS3Draft) SoftLineBreak(thisLastRune rune, siblingF return true } -type eastAsianLineBreaks struct { - Enabled bool - EastAsianLineBreaksFunction eastAsianLineBreaker -} - type withEastAsianLineBreaks struct { - eastAsianLineBreaksStyle EastAsianLineBreaksStyle + eastAsianLineBreaksStyle EastAsianLineBreaks } func (o *withEastAsianLineBreaks) SetConfig(c *renderer.Config) { - switch o.eastAsianLineBreaksStyle { - case EastAsianLineBreaksStyleSimple: - c.Options[optEastAsianLineBreaks] = eastAsianLineBreaks{ - Enabled: true, - EastAsianLineBreaksFunction: &eastAsianLineBreaksSimple{}, - } - case EastAsianLineBreaksCSS3Draft: - c.Options[optEastAsianLineBreaks] = eastAsianLineBreaks{ - Enabled: true, - EastAsianLineBreaksFunction: &eastAsianLineBreaksCSS3Draft{}, - } - } + c.Options[optEastAsianLineBreaks] = o.eastAsianLineBreaksStyle } func (o *withEastAsianLineBreaks) SetHTMLOption(c *Config) { - switch o.eastAsianLineBreaksStyle { - case EastAsianLineBreaksStyleSimple: - c.EastAsianLineBreaks = eastAsianLineBreaks{ - Enabled: true, - EastAsianLineBreaksFunction: &eastAsianLineBreaksSimple{}, - } - case EastAsianLineBreaksCSS3Draft: - c.EastAsianLineBreaks = eastAsianLineBreaks{ - Enabled: true, - EastAsianLineBreaksFunction: &eastAsianLineBreaksCSS3Draft{}, - } - } + c.EastAsianLineBreaks = o.eastAsianLineBreaksStyle } // WithEastAsianLineBreaks is a functional option that indicates whether softline breaks // between east asian wide characters should be ignored. -func WithEastAsianLineBreaks(style EastAsianLineBreaksStyle) interface { +func WithEastAsianLineBreaks(e EastAsianLineBreaks) interface { renderer.Option Option } { - return &withEastAsianLineBreaks{style} + return &withEastAsianLineBreaks{e} } // XHTML is an option name used in WithXHTML. @@ -759,13 +734,13 @@ func (r *Renderer) renderText(w util.BufWriter, source []byte, node ast.Node, en _, _ = w.WriteString("
\n") } } else if n.SoftLineBreak() { - if r.EastAsianLineBreaks.Enabled && len(value) != 0 { + if r.EastAsianLineBreaks != EastAsianLineBreaksNone && len(value) != 0 { sibling := node.NextSibling() if sibling != nil && sibling.Kind() == ast.KindText { if siblingText := sibling.(*ast.Text).Text(source); len(siblingText) != 0 { thisLastRune := util.ToRune(value, len(value)-1) siblingFirstRune, _ := utf8.DecodeRune(siblingText) - if r.EastAsianLineBreaks.EastAsianLineBreaksFunction.SoftLineBreak(thisLastRune, siblingFirstRune) { + if r.EastAsianLineBreaks.softLineBreak(thisLastRune, siblingFirstRune) { _ = w.WriteByte('\n') } }