1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:24:54 -07:00

present: allow markup inside punctuation connected to other text

This allows markup bracketed by punctuation even when the
punctuation has text on the other side, like in:

- Markup—_especially_italic_text_—can easily be overused.
- We want to increase `go`vet`'s usage.

Change-Id: I0c6ca790f23f705d8c8ba8a225c0280b916ebb6c
Reviewed-on: https://go-review.googlesource.com/33662
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Russ Cox 2016-11-29 13:39:51 -05:00
parent e5f9a3deee
commit e04df2157a
3 changed files with 29 additions and 12 deletions

View File

@ -77,14 +77,22 @@ Fonts:
Within the input for plain text or lists, text bracketed by font Within the input for plain text or lists, text bracketed by font
markers will be presented in italic, bold, or program font. markers will be presented in italic, bold, or program font.
Marker characters are _ (italic), * (bold) and ` (program font). Marker characters are _ (italic), * (bold) and ` (program font).
Unmatched markers appear as plain text. An opening marker must be preceded by a space or punctuation
Within marked text, a single marker character becomes a space character or else be at start of a line; similarly, a closing
and a doubled single marker quotes the marker character. marker must be followed by a space or punctuation character or
else be at the end of a line. Unmatched markers appear as plain text.
There must be no spaces between markers. Within marked text,
a single marker character becomes a space and a doubled single
marker quotes the marker character.
at the beginning of a line or
else be preceded by a space or punctuation; similarly a closing
marker must be at the end of the lineo
_italic_ _italic_
*bold* *bold*
`program` `program`
_this_is_all_italic_ Markup_especially_italic_text_can easily be overused.
_Why_use_scoped__ptr_? Use plain ***ptr* instead. _Why_use_scoped__ptr_? Use plain ***ptr* instead.
Inline links: Inline links:

View File

@ -52,16 +52,16 @@ Word:
words[w] = link words[w] = link
continue Word continue Word
} }
const punctuation = `.,;:()!?—–'"`
const marker = "_*`" const marker = "_*`"
// Initial punctuation is OK but must be peeled off. // Initial punctuation is OK but must be peeled off.
first := strings.IndexAny(word, marker) first := strings.IndexAny(word, marker)
if first == -1 { if first == -1 {
continue Word continue Word
} }
// Is the marker prefixed only by punctuation? // Opening marker must be at the beginning of the token or else preceded by punctuation.
for _, r := range word[:first] { if first != 0 {
if !strings.ContainsRune(punctuation, r) { r, _ := utf8.DecodeLastRuneInString(word[:first])
if !unicode.IsPunct(r) {
continue Word continue Word
} }
} }
@ -81,17 +81,18 @@ Word:
open += "<code>" open += "<code>"
close = "</code>" close = "</code>"
} }
// Terminal punctuation is OK but must be peeled off. // Closing marker must be at the end of the token or else followed by punctuation.
last := strings.LastIndex(word, word[:1]) last := strings.LastIndex(word, word[:1])
if last == 0 { if last == 0 {
continue Word continue Word
} }
head, tail := word[:last+1], word[last+1:] if last+1 != len(word) {
for _, r := range tail { r, _ := utf8.DecodeRuneInString(word[last+1:])
if !strings.ContainsRune(punctuation, r) { if !unicode.IsPunct(r) {
continue Word continue Word
} }
} }
head, tail := word[:last+1], word[last+1:]
b.Reset() b.Reset()
b.WriteString(open) b.WriteString(open)
var wid int var wid int

View File

@ -72,6 +72,14 @@ func TestFont(t *testing.T) {
`Visit <a href="http://golang.org" target="_blank">golang.org</a> now`}, `Visit <a href="http://golang.org" target="_blank">golang.org</a> now`},
{"my talk ([[http://talks.golang.org/][slides here]])", {"my talk ([[http://talks.golang.org/][slides here]])",
`my talk (<a href="http://talks.golang.org/" target="_blank">slides here</a>)`}, `my talk (<a href="http://talks.golang.org/" target="_blank">slides here</a>)`},
{"Markup—_especially_italic_text_—can easily be overused.",
`Markup—<i>especially italic text</i>—can easily be overused.`},
{"`go`get`'s codebase", // ascii U+0027 ' before s
`<code>go get</code>'s codebase`},
{"`go`get`s codebase", // unicode right single quote U+2019 before s
`<code>go get</code>s codebase`},
{"a_variable_name",
`a_variable_name`},
} }
for _, test := range tests { for _, test := range tests {
out := font(test.in) out := font(test.in)