Пример #1
0
func ruleEntity(s *StateInline, silent bool) (_ bool) {
	pos := s.Pos
	src := s.Src

	if src[pos] != '&' {
		return
	}

	max := s.PosMax

	if pos+1 < max {
		if e, n := html.ParseEntity(src[pos:]); n > 0 {
			if !silent {
				s.Pending.WriteString(e)
			}
			s.Pos += n
			return true
		}
	}

	if !silent {
		s.Pending.WriteByte('&')
	}
	s.Pos++

	return true
}
Пример #2
0
func unescapeAll(s string) string {
	anyChanges := false
	i := 0
	for i < len(s)-1 {
		b := s[i]
		if b == '\\' {
			if mdpunct[s[i+1]] {
				anyChanges = true
				break
			}
		} else if b == '&' {
			if _, n := html.ParseEntity(s[i:]); n > 0 {
				anyChanges = true
				break
			}
		}
		i++
	}

	if !anyChanges {
		return s
	}

	buf := make([]byte, len(s))
	copy(buf[:i], s)
	j := i
	for i < len(s) {
		b := s[i]
		if b == '\\' {
			if i+1 < len(s) {
				b = s[i+1]
				if mdpunct[b] {
					buf[j] = b
					j++
				} else {
					buf[j] = '\\'
					j++
					buf[j] = b
					j++
				}
				i += 2
				continue
			}
		} else if b == '&' {
			if e, n := html.ParseEntity(s[i:]); n > 0 {
				if len(e) > n && len(buf) == len(s) {
					newBuf := make([]byte, cap(buf)*2)
					copy(newBuf[:j], buf)
					buf = newBuf
				}
				j += copy(buf[j:], e)
				i += n
				continue
			}
		}
		buf[j] = b
		j++
		i++
	}

	return string(buf[:j])
}