mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
Merge pull request #88 from tangiel/apostrophe
typographer: add basic support for apostrophes
This commit is contained in:
commit
faaafa55b6
2 changed files with 32 additions and 0 deletions
|
|
@ -18,3 +18,10 @@ This should "be" replaced
|
||||||
//- - - - - - - - -//
|
//- - - - - - - - -//
|
||||||
<p><strong>–</strong> <em>—</em> a…« b»</p>
|
<p><strong>–</strong> <em>—</em> a…« b»</p>
|
||||||
//= = = = = = = = = = = = = = = = = = = = = = = =//
|
//= = = = = = = = = = = = = = = = = = = = = = = =//
|
||||||
|
|
||||||
|
4
|
||||||
|
//- - - - - - - - -//
|
||||||
|
Some say '90s, others say 90's, but I can't say which is best.
|
||||||
|
//- - - - - - - - -//
|
||||||
|
<p>Some say ’90s, others say 90’s, but I can’t say which is best.</p>
|
||||||
|
//= = = = = = = = = = = = = = = = = = = = = = = =//
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package extension
|
package extension
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
gast "github.com/yuin/goldmark/ast"
|
gast "github.com/yuin/goldmark/ast"
|
||||||
"github.com/yuin/goldmark/parser"
|
"github.com/yuin/goldmark/parser"
|
||||||
|
|
@ -31,6 +33,8 @@ const (
|
||||||
LeftAngleQuote
|
LeftAngleQuote
|
||||||
// RightAngleQuote is >>
|
// RightAngleQuote is >>
|
||||||
RightAngleQuote
|
RightAngleQuote
|
||||||
|
// Apostrophe is '
|
||||||
|
Apostrophe
|
||||||
|
|
||||||
typographicPunctuationMax
|
typographicPunctuationMax
|
||||||
)
|
)
|
||||||
|
|
@ -52,6 +56,7 @@ func newDefaultSubstitutions() [][]byte {
|
||||||
replacements[Ellipsis] = []byte("…")
|
replacements[Ellipsis] = []byte("…")
|
||||||
replacements[LeftAngleQuote] = []byte("«")
|
replacements[LeftAngleQuote] = []byte("«")
|
||||||
replacements[RightAngleQuote] = []byte("»")
|
replacements[RightAngleQuote] = []byte("»")
|
||||||
|
replacements[Apostrophe] = []byte("’")
|
||||||
|
|
||||||
return replacements
|
return replacements
|
||||||
}
|
}
|
||||||
|
|
@ -189,6 +194,26 @@ func (s *typographerParser) Parse(parent gast.Node, block text.Reader, pc parser
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if c == '\'' {
|
if c == '\'' {
|
||||||
|
if s.Substitutions[Apostrophe] != nil {
|
||||||
|
// Handle decade abbrevations such as '90s
|
||||||
|
if d.CanOpen && !d.CanClose && len(line) > 3 && util.IsNumeric(line[1]) && util.IsNumeric(line[2]) && line[3] == 's' {
|
||||||
|
after := util.ToRune(line, 4)
|
||||||
|
if len(line) == 3 || unicode.IsSpace(after) || unicode.IsPunct(after) {
|
||||||
|
node := gast.NewString(s.Substitutions[Apostrophe])
|
||||||
|
node.SetCode(true)
|
||||||
|
block.Advance(1)
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Convert normal apostrophes. This is probably more flexible than necessary but
|
||||||
|
// converts any apostrophe in between two alphanumerics.
|
||||||
|
if len(line) > 1 && (unicode.IsDigit(before) || unicode.IsLetter(before)) && (util.IsAlphaNumeric(line[1])) {
|
||||||
|
node := gast.NewString(s.Substitutions[Apostrophe])
|
||||||
|
node.SetCode(true)
|
||||||
|
block.Advance(1)
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
}
|
||||||
if s.Substitutions[LeftSingleQuote] != nil && d.CanOpen && !d.CanClose {
|
if s.Substitutions[LeftSingleQuote] != nil && d.CanOpen && !d.CanClose {
|
||||||
node := gast.NewString(s.Substitutions[LeftSingleQuote])
|
node := gast.NewString(s.Substitutions[LeftSingleQuote])
|
||||||
node.SetCode(true)
|
node.SetCode(true)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue