-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define line break styles for east asian characters as options #411
Changes from 7 commits
6ef9b10
6cbcfeb
2367b9f
dc2230c
9d0b1b6
792af68
8c6830d
6b3067e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ import ( | |
type Config struct { | ||
Writer Writer | ||
HardWraps bool | ||
EastAsianLineBreaks bool | ||
EastAsianLineBreaks eastAsianLineBreaks | ||
XHTML bool | ||
Unsafe bool | ||
} | ||
|
@@ -26,7 +26,7 @@ func NewConfig() Config { | |
return Config{ | ||
Writer: DefaultWriter, | ||
HardWraps: false, | ||
EastAsianLineBreaks: false, | ||
EastAsianLineBreaks: eastAsianLineBreaks{}, | ||
XHTML: false, | ||
Unsafe: false, | ||
} | ||
|
@@ -38,7 +38,7 @@ func (c *Config) SetOption(name renderer.OptionName, value interface{}) { | |
case optHardWraps: | ||
c.HardWraps = value.(bool) | ||
case optEastAsianLineBreaks: | ||
c.EastAsianLineBreaks = value.(bool) | ||
c.EastAsianLineBreaks = value.(eastAsianLineBreaks) | ||
case optXHTML: | ||
c.XHTML = value.(bool) | ||
case optUnsafe: | ||
|
@@ -103,24 +103,80 @@ 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 | ||
|
||
const ( | ||
// EastAsianLineBreaksStyleSimple is a style where soft line breaks are ignored | ||
// if both sides of the break are east asian wide characters. | ||
EastAsianLineBreaksStyleSimple EastAsianLineBreaksStyle = iota | ||
// 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 | ||
) | ||
|
||
type eastAsianLineBreaksFunction interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name does not meet conventions in Go. For this kind of interfaces that has only one method, we often define a factory function (i.e. |
||
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)) | ||
} | ||
|
||
type eastAsianLineBreaksCSS3Draft struct{} | ||
|
||
func (e *eastAsianLineBreaksCSS3Draft) SoftLineBreak(thisLastRune rune, siblingFirstRune rune) bool { | ||
return !(util.IsEastAsianWideRune(thisLastRune) || util.IsEastAsianWideRune(siblingFirstRune)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this does not satisfy CSS3 Draft rules.
So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll try implementing based on the provided direction, though I'm not fully confident I've grasped all the details (still wrapping my head around Unicode handling). Might need your patience and continued reviews – really appreciate it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After reviewing the CSS Text Module Level 3, it appears the current usage is as shown in the following screenshot. May I proceed to implement the specifications (+ enhancements) you pointed out as
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Some implementation of this algorithm may be helpful for you
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the information. I will begin implementing |
||
} | ||
|
||
type eastAsianLineBreaks struct { | ||
Enabled bool | ||
EastAsianLineBreaksFunction eastAsianLineBreaksFunction | ||
} | ||
|
||
type withEastAsianLineBreaks struct { | ||
eastAsianLineBreaksStyle EastAsianLineBreaksStyle | ||
} | ||
|
||
func (o *withEastAsianLineBreaks) SetConfig(c *renderer.Config) { | ||
c.Options[optEastAsianLineBreaks] = true | ||
switch o.eastAsianLineBreaksStyle { | ||
case EastAsianLineBreaksStyleSimple: | ||
c.Options[optEastAsianLineBreaks] = eastAsianLineBreaks{ | ||
Enabled: true, | ||
EastAsianLineBreaksFunction: &eastAsianLineBreaksSimple{}, | ||
} | ||
case EastAsianLineBreaksCSS3Draft: | ||
c.Options[optEastAsianLineBreaks] = eastAsianLineBreaks{ | ||
Enabled: true, | ||
EastAsianLineBreaksFunction: &eastAsianLineBreaksCSS3Draft{}, | ||
} | ||
} | ||
} | ||
|
||
func (o *withEastAsianLineBreaks) SetHTMLOption(c *Config) { | ||
c.EastAsianLineBreaks = true | ||
switch o.eastAsianLineBreaksStyle { | ||
case EastAsianLineBreaksStyleSimple: | ||
c.EastAsianLineBreaks = eastAsianLineBreaks{ | ||
Enabled: true, | ||
EastAsianLineBreaksFunction: &eastAsianLineBreaksSimple{}, | ||
} | ||
case EastAsianLineBreaksCSS3Draft: | ||
c.EastAsianLineBreaks = eastAsianLineBreaks{ | ||
Enabled: true, | ||
EastAsianLineBreaksFunction: &eastAsianLineBreaksCSS3Draft{}, | ||
} | ||
} | ||
} | ||
|
||
// WithEastAsianLineBreaks is a functional option that indicates whether softline breaks | ||
// between east asian wide characters should be ignored. | ||
func WithEastAsianLineBreaks() interface { | ||
func WithEastAsianLineBreaks(style EastAsianLineBreaksStyle) interface { | ||
renderer.Option | ||
Option | ||
} { | ||
return &withEastAsianLineBreaks{} | ||
return &withEastAsianLineBreaks{style} | ||
} | ||
|
||
// XHTML is an option name used in WithXHTML. | ||
|
@@ -663,14 +719,13 @@ func (r *Renderer) renderText(w util.BufWriter, source []byte, node ast.Node, en | |
_, _ = w.WriteString("<br>\n") | ||
} | ||
} else if n.SoftLineBreak() { | ||
if r.EastAsianLineBreaks && len(value) != 0 { | ||
if r.EastAsianLineBreaks.Enabled && 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 !(util.IsEastAsianWideRune(thisLastRune) && | ||
util.IsEastAsianWideRune(siblingFirstRune)) { | ||
if r.EastAsianLineBreaks.EastAsianLineBreaksFunction.SoftLineBreak(thisLastRune, siblingFirstRune) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am convinced that this is one of the things we should implement. |
||
_ = w.WriteByte('\n') | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a better name than
EastAsianLineBreaksCSS3Draft
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems your implementation does not satisfy CSS3 draft rules. We may have choices...
I prefer 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your thorough feedback.
I’m on board with option 1 and will give the CSS text level 3 rules and additional enhancements a shot. Admittedly, I'm not a pro with this CSS issue, so while I'll do my best, I might miss some nuances. Any extra guidance or pointers while I work through this would be awesome!
Will keep you posted on the progress. Talk to you soon!