all repos — grayfriday @ b0bdfbec4ceab22844aa766b3856aa95753ffde8

blackfriday fork with a few changes

markdown.go (view raw)

  1//
  2// Blackfriday Markdown Processor
  3// Available at http://github.com/russross/blackfriday
  4//
  5// Copyright © 2011 Russ Ross <russ@russross.com>.
  6// Distributed under the Simplified BSD License.
  7// See README.md for details.
  8//
  9
 10//
 11//
 12// Markdown parsing and processing
 13//
 14//
 15
 16// Blackfriday markdown processor.
 17//
 18// Translates plain text with simple formatting rules into HTML or LaTeX.
 19package blackfriday
 20
 21import (
 22	"bytes"
 23	"unicode/utf8"
 24)
 25
 26const VERSION = "1.1"
 27
 28// These are the supported markdown parsing extensions.
 29// OR these values together to select multiple extensions.
 30const (
 31	EXTENSION_NO_INTRA_EMPHASIS          = 1 << iota // ignore emphasis markers inside words
 32	EXTENSION_TABLES                                 // render tables
 33	EXTENSION_FENCED_CODE                            // render fenced code blocks
 34	EXTENSION_AUTOLINK                               // detect embedded URLs that are not explicitly marked
 35	EXTENSION_STRIKETHROUGH                          // strikethrough text using ~~test~~
 36	EXTENSION_LAX_HTML_BLOCKS                        // loosen up HTML block parsing rules
 37	EXTENSION_SPACE_HEADERS                          // be strict about prefix header rules
 38	EXTENSION_HARD_LINE_BREAK                        // translate newlines into line breaks
 39	EXTENSION_TAB_SIZE_EIGHT                         // expand tabs to eight spaces instead of four
 40	EXTENSION_FOOTNOTES                              // Pandoc-style footnotes
 41	EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK             // No need to insert an empty line to start a (code, quote, order list, unorder list)block
 42)
 43
 44// These are the possible flag values for the link renderer.
 45// Only a single one of these values will be used; they are not ORed together.
 46// These are mostly of interest if you are writing a new output format.
 47const (
 48	LINK_TYPE_NOT_AUTOLINK = iota
 49	LINK_TYPE_NORMAL
 50	LINK_TYPE_EMAIL
 51)
 52
 53// These are the possible flag values for the ListItem renderer.
 54// Multiple flag values may be ORed together.
 55// These are mostly of interest if you are writing a new output format.
 56const (
 57	LIST_TYPE_ORDERED = 1 << iota
 58	LIST_ITEM_CONTAINS_BLOCK
 59	LIST_ITEM_BEGINNING_OF_LIST
 60	LIST_ITEM_END_OF_LIST
 61)
 62
 63// These are the possible flag values for the table cell renderer.
 64// Only a single one of these values will be used; they are not ORed together.
 65// These are mostly of interest if you are writing a new output format.
 66const (
 67	TABLE_ALIGNMENT_LEFT = 1 << iota
 68	TABLE_ALIGNMENT_RIGHT
 69	TABLE_ALIGNMENT_CENTER = (TABLE_ALIGNMENT_LEFT | TABLE_ALIGNMENT_RIGHT)
 70)
 71
 72// The size of a tab stop.
 73const (
 74	TAB_SIZE_DEFAULT = 4
 75	TAB_SIZE_EIGHT   = 8
 76)
 77
 78// These are the tags that are recognized as HTML block tags.
 79// Any of these can be included in markdown text without special escaping.
 80var blockTags = map[string]bool{
 81	"p":          true,
 82	"dl":         true,
 83	"h1":         true,
 84	"h2":         true,
 85	"h3":         true,
 86	"h4":         true,
 87	"h5":         true,
 88	"h6":         true,
 89	"ol":         true,
 90	"ul":         true,
 91	"del":        true,
 92	"div":        true,
 93	"ins":        true,
 94	"pre":        true,
 95	"form":       true,
 96	"math":       true,
 97	"table":      true,
 98	"iframe":     true,
 99	"script":     true,
100	"fieldset":   true,
101	"noscript":   true,
102	"blockquote": true,
103
104	// HTML5
105	"video":      true,
106	"aside":      true,
107	"canvas":     true,
108	"figure":     true,
109	"footer":     true,
110	"header":     true,
111	"hgroup":     true,
112	"output":     true,
113	"article":    true,
114	"section":    true,
115	"progress":   true,
116	"figcaption": true,
117}
118
119// Renderer is the rendering interface.
120// This is mostly of interest if you are implementing a new rendering format.
121//
122// When a byte slice is provided, it contains the (rendered) contents of the
123// element.
124//
125// When a callback is provided instead, it will write the contents of the
126// respective element directly to the output buffer and return true on success.
127// If the callback returns false, the rendering function should reset the
128// output buffer as though it had never been called.
129//
130// Currently Html and Latex implementations are provided
131type Renderer interface {
132	// block-level callbacks
133	BlockCode(out *bytes.Buffer, text []byte, lang string)
134	BlockQuote(out *bytes.Buffer, text []byte)
135	BlockHtml(out *bytes.Buffer, text []byte)
136	Header(out *bytes.Buffer, text func() bool, level int)
137	HRule(out *bytes.Buffer)
138	List(out *bytes.Buffer, text func() bool, flags int)
139	ListItem(out *bytes.Buffer, text []byte, flags int)
140	Paragraph(out *bytes.Buffer, text func() bool)
141	Table(out *bytes.Buffer, header []byte, body []byte, columnData []int)
142	TableRow(out *bytes.Buffer, text []byte)
143	TableHeaderCell(out *bytes.Buffer, text []byte, flags int)
144	TableCell(out *bytes.Buffer, text []byte, flags int)
145	Footnotes(out *bytes.Buffer, text func() bool)
146	FootnoteItem(out *bytes.Buffer, name, text []byte, flags int)
147
148	// Span-level callbacks
149	AutoLink(out *bytes.Buffer, link []byte, kind int)
150	CodeSpan(out *bytes.Buffer, text []byte)
151	DoubleEmphasis(out *bytes.Buffer, text []byte)
152	Emphasis(out *bytes.Buffer, text []byte)
153	Image(out *bytes.Buffer, link []byte, title []byte, alt []byte)
154	LineBreak(out *bytes.Buffer)
155	Link(out *bytes.Buffer, link []byte, title []byte, content []byte)
156	RawHtmlTag(out *bytes.Buffer, tag []byte)
157	TripleEmphasis(out *bytes.Buffer, text []byte)
158	StrikeThrough(out *bytes.Buffer, text []byte)
159	FootnoteRef(out *bytes.Buffer, ref []byte, id int)
160
161	// Low-level callbacks
162	Entity(out *bytes.Buffer, entity []byte)
163	NormalText(out *bytes.Buffer, text []byte)
164
165	// Header and footer
166	DocumentHeader(out *bytes.Buffer)
167	DocumentFooter(out *bytes.Buffer)
168
169	GetFlags() int
170}
171
172// Callback functions for inline parsing. One such function is defined
173// for each character that triggers a response when parsing inline data.
174type inlineParser func(p *parser, out *bytes.Buffer, data []byte, offset int) int
175
176// Parser holds runtime state used by the parser.
177// This is constructed by the Markdown function.
178type parser struct {
179	r              Renderer
180	refs           map[string]*reference
181	inlineCallback [256]inlineParser
182	flags          int
183	nesting        int
184	maxNesting     int
185	insideLink     bool
186
187	// Footnotes need to be ordered as well as available to quickly check for
188	// presence. If a ref is also a footnote, it's stored both in refs and here
189	// in notes. Slice is nil if footnotes not enabled.
190	notes []*reference
191}
192
193//
194//
195// Public interface
196//
197//
198
199// MarkdownBasic is a convenience function for simple rendering.
200// It processes markdown input with no extensions enabled.
201func MarkdownBasic(input []byte) []byte {
202	// set up the HTML renderer
203	htmlFlags := HTML_USE_XHTML
204	renderer := HtmlRenderer(htmlFlags, "", "")
205
206	// set up the parser
207	extensions := 0
208
209	return Markdown(input, renderer, extensions)
210}
211
212// Call Markdown with most useful extensions enabled
213// MarkdownCommon is a convenience function for simple rendering.
214// It processes markdown input with common extensions enabled, including:
215//
216// * Smartypants processing with smart fractions and LaTeX dashes
217//
218// * Intra-word emphasis suppression
219//
220// * Tables
221//
222// * Fenced code blocks
223//
224// * Autolinking
225//
226// * Strikethrough support
227//
228// * Strict header parsing
229func MarkdownCommon(input []byte) []byte {
230	// set up the HTML renderer
231	htmlFlags := 0
232	htmlFlags |= HTML_USE_XHTML
233	htmlFlags |= HTML_USE_SMARTYPANTS
234	htmlFlags |= HTML_SMARTYPANTS_FRACTIONS
235	htmlFlags |= HTML_SMARTYPANTS_LATEX_DASHES
236	htmlFlags |= HTML_SANITIZE_OUTPUT
237	renderer := HtmlRenderer(htmlFlags, "", "")
238
239	// set up the parser
240	extensions := 0
241	extensions |= EXTENSION_NO_INTRA_EMPHASIS
242	extensions |= EXTENSION_TABLES
243	extensions |= EXTENSION_FENCED_CODE
244	extensions |= EXTENSION_AUTOLINK
245	extensions |= EXTENSION_STRIKETHROUGH
246	extensions |= EXTENSION_SPACE_HEADERS
247
248	return Markdown(input, renderer, extensions)
249}
250
251// Markdown is the main rendering function.
252// It parses and renders a block of markdown-encoded text.
253// The supplied Renderer is used to format the output, and extensions dictates
254// which non-standard extensions are enabled.
255//
256// To use the supplied Html or LaTeX renderers, see HtmlRenderer and
257// LatexRenderer, respectively.
258func Markdown(input []byte, renderer Renderer, extensions int) []byte {
259	// no point in parsing if we can't render
260	if renderer == nil {
261		return nil
262	}
263
264	// fill in the render structure
265	p := new(parser)
266	p.r = renderer
267	p.flags = extensions
268	p.refs = make(map[string]*reference)
269	p.maxNesting = 16
270	p.insideLink = false
271
272	// register inline parsers
273	p.inlineCallback['*'] = emphasis
274	p.inlineCallback['_'] = emphasis
275	if extensions&EXTENSION_STRIKETHROUGH != 0 {
276		p.inlineCallback['~'] = emphasis
277	}
278	p.inlineCallback['`'] = codeSpan
279	p.inlineCallback['\n'] = lineBreak
280	p.inlineCallback['['] = link
281	p.inlineCallback['<'] = leftAngle
282	p.inlineCallback['\\'] = escape
283	p.inlineCallback['&'] = entity
284
285	if extensions&EXTENSION_AUTOLINK != 0 {
286		p.inlineCallback[':'] = autoLink
287	}
288
289	if extensions&EXTENSION_FOOTNOTES != 0 {
290		p.notes = make([]*reference, 0)
291	}
292
293	first := firstPass(p, input)
294	second := secondPass(p, first)
295
296	if renderer.GetFlags()&HTML_SANITIZE_OUTPUT != 0 {
297		second = sanitizeHtml(second)
298	}
299
300	return second
301}
302
303// first pass:
304// - extract references
305// - expand tabs
306// - normalize newlines
307// - copy everything else
308func firstPass(p *parser, input []byte) []byte {
309	var out bytes.Buffer
310	tabSize := TAB_SIZE_DEFAULT
311	if p.flags&EXTENSION_TAB_SIZE_EIGHT != 0 {
312		tabSize = TAB_SIZE_EIGHT
313	}
314	beg, end := 0, 0
315	for beg < len(input) { // iterate over lines
316		if end = isReference(p, input[beg:], tabSize); end > 0 {
317			beg += end
318		} else { // skip to the next line
319			end = beg
320			for end < len(input) && input[end] != '\n' && input[end] != '\r' {
321				end++
322			}
323
324			// add the line body if present
325			if end > beg {
326				expandTabs(&out, input[beg:end], tabSize)
327			}
328			out.WriteByte('\n')
329
330			if end < len(input) && input[end] == '\r' {
331				end++
332			}
333			if end < len(input) && input[end] == '\n' {
334				end++
335			}
336
337			beg = end
338		}
339	}
340
341	// empty input?
342	if out.Len() == 0 {
343		out.WriteByte('\n')
344	}
345
346	return out.Bytes()
347}
348
349// second pass: actual rendering
350func secondPass(p *parser, input []byte) []byte {
351	var output bytes.Buffer
352
353	p.r.DocumentHeader(&output)
354	p.block(&output, input)
355
356	if p.flags&EXTENSION_FOOTNOTES != 0 && len(p.notes) > 0 {
357		p.r.Footnotes(&output, func() bool {
358			flags := LIST_ITEM_BEGINNING_OF_LIST
359			for _, ref := range p.notes {
360				var buf bytes.Buffer
361				if ref.hasBlock {
362					flags |= LIST_ITEM_CONTAINS_BLOCK
363					p.block(&buf, ref.title)
364				} else {
365					p.inline(&buf, ref.title)
366				}
367				p.r.FootnoteItem(&output, ref.link, buf.Bytes(), flags)
368				flags &^= LIST_ITEM_BEGINNING_OF_LIST | LIST_ITEM_CONTAINS_BLOCK
369			}
370
371			return true
372		})
373	}
374
375	p.r.DocumentFooter(&output)
376
377	if p.nesting != 0 {
378		panic("Nesting level did not end at zero")
379	}
380
381	return output.Bytes()
382}
383
384//
385// Link references
386//
387// This section implements support for references that (usually) appear
388// as footnotes in a document, and can be referenced anywhere in the document.
389// The basic format is:
390//
391//    [1]: http://www.google.com/ "Google"
392//    [2]: http://www.github.com/ "Github"
393//
394// Anywhere in the document, the reference can be linked by referring to its
395// label, i.e., 1 and 2 in this example, as in:
396//
397//    This library is hosted on [Github][2], a git hosting site.
398//
399// Actual footnotes as specified in Pandoc and supported by some other Markdown
400// libraries such as php-markdown are also taken care of. They look like this:
401//
402//    This sentence needs a bit of further explanation.[^note]
403//
404//    [^note]: This is the explanation.
405//
406// Footnotes should be placed at the end of the document in an ordered list.
407// Inline footnotes such as:
408//
409//    Inline footnotes^[Not supported.] also exist.
410//
411// are not yet supported.
412
413// References are parsed and stored in this struct.
414type reference struct {
415	link     []byte
416	title    []byte
417	noteId   int // 0 if not a footnote ref
418	hasBlock bool
419}
420
421// Check whether or not data starts with a reference link.
422// If so, it is parsed and stored in the list of references
423// (in the render struct).
424// Returns the number of bytes to skip to move past it,
425// or zero if the first line is not a reference.
426func isReference(p *parser, data []byte, tabSize int) int {
427	// up to 3 optional leading spaces
428	if len(data) < 4 {
429		return 0
430	}
431	i := 0
432	for i < 3 && data[i] == ' ' {
433		i++
434	}
435
436	noteId := 0
437
438	// id part: anything but a newline between brackets
439	if data[i] != '[' {
440		return 0
441	}
442	i++
443	if p.flags&EXTENSION_FOOTNOTES != 0 {
444		if data[i] == '^' {
445			// we can set it to anything here because the proper noteIds will
446			// be assigned later during the second pass. It just has to be != 0
447			noteId = 1
448			i++
449		}
450	}
451	idOffset := i
452	for i < len(data) && data[i] != '\n' && data[i] != '\r' && data[i] != ']' {
453		i++
454	}
455	if i >= len(data) || data[i] != ']' {
456		return 0
457	}
458	idEnd := i
459
460	// spacer: colon (space | tab)* newline? (space | tab)*
461	i++
462	if i >= len(data) || data[i] != ':' {
463		return 0
464	}
465	i++
466	for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
467		i++
468	}
469	if i < len(data) && (data[i] == '\n' || data[i] == '\r') {
470		i++
471		if i < len(data) && data[i] == '\n' && data[i-1] == '\r' {
472			i++
473		}
474	}
475	for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
476		i++
477	}
478	if i >= len(data) {
479		return 0
480	}
481
482	var (
483		linkOffset, linkEnd   int
484		titleOffset, titleEnd int
485		lineEnd               int
486		raw                   []byte
487		hasBlock              bool
488	)
489
490	if p.flags&EXTENSION_FOOTNOTES != 0 && noteId != 0 {
491		linkOffset, linkEnd, raw, hasBlock = scanFootnote(p, data, i, tabSize)
492		lineEnd = linkEnd
493	} else {
494		linkOffset, linkEnd, titleOffset, titleEnd, lineEnd = scanLinkRef(p, data, i)
495	}
496	if lineEnd == 0 {
497		return 0
498	}
499
500	// a valid ref has been found
501
502	ref := &reference{
503		noteId:   noteId,
504		hasBlock: hasBlock,
505	}
506
507	if noteId > 0 {
508		// reusing the link field for the id since footnotes don't have links
509		ref.link = data[idOffset:idEnd]
510		// if footnote, it's not really a title, it's the contained text
511		ref.title = raw
512	} else {
513		ref.link = data[linkOffset:linkEnd]
514		ref.title = data[titleOffset:titleEnd]
515	}
516
517	// id matches are case-insensitive
518	id := string(bytes.ToLower(data[idOffset:idEnd]))
519
520	p.refs[id] = ref
521
522	return lineEnd
523}
524
525func scanLinkRef(p *parser, data []byte, i int) (linkOffset, linkEnd, titleOffset, titleEnd, lineEnd int) {
526	// link: whitespace-free sequence, optionally between angle brackets
527	if data[i] == '<' {
528		i++
529	}
530	linkOffset = i
531	for i < len(data) && data[i] != ' ' && data[i] != '\t' && data[i] != '\n' && data[i] != '\r' {
532		i++
533	}
534	linkEnd = i
535	if data[linkOffset] == '<' && data[linkEnd-1] == '>' {
536		linkOffset++
537		linkEnd--
538	}
539
540	// optional spacer: (space | tab)* (newline | '\'' | '"' | '(' )
541	for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
542		i++
543	}
544	if i < len(data) && data[i] != '\n' && data[i] != '\r' && data[i] != '\'' && data[i] != '"' && data[i] != '(' {
545		return
546	}
547
548	// compute end-of-line
549	if i >= len(data) || data[i] == '\r' || data[i] == '\n' {
550		lineEnd = i
551	}
552	if i+1 < len(data) && data[i] == '\r' && data[i+1] == '\n' {
553		lineEnd++
554	}
555
556	// optional (space|tab)* spacer after a newline
557	if lineEnd > 0 {
558		i = lineEnd + 1
559		for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
560			i++
561		}
562	}
563
564	// optional title: any non-newline sequence enclosed in '"() alone on its line
565	if i+1 < len(data) && (data[i] == '\'' || data[i] == '"' || data[i] == '(') {
566		i++
567		titleOffset = i
568
569		// look for EOL
570		for i < len(data) && data[i] != '\n' && data[i] != '\r' {
571			i++
572		}
573		if i+1 < len(data) && data[i] == '\n' && data[i+1] == '\r' {
574			titleEnd = i + 1
575		} else {
576			titleEnd = i
577		}
578
579		// step back
580		i--
581		for i > titleOffset && (data[i] == ' ' || data[i] == '\t') {
582			i--
583		}
584		if i > titleOffset && (data[i] == '\'' || data[i] == '"' || data[i] == ')') {
585			lineEnd = titleEnd
586			titleEnd = i
587		}
588	}
589
590	return
591}
592
593// The first bit of this logic is the same as (*parser).listItem, but the rest
594// is much simpler. This function simply finds the entire block and shifts it
595// over by one tab if it is indeed a block (just returns the line if it's not).
596// blockEnd is the end of the section in the input buffer, and contents is the
597// extracted text that was shifted over one tab. It will need to be rendered at
598// the end of the document.
599func scanFootnote(p *parser, data []byte, i, indentSize int) (blockStart, blockEnd int, contents []byte, hasBlock bool) {
600	if i == 0 || len(data) == 0 {
601		return
602	}
603
604	// skip leading whitespace on first line
605	for i < len(data) && data[i] == ' ' {
606		i++
607	}
608
609	blockStart = i
610
611	// find the end of the line
612	blockEnd = i
613	for i < len(data) && data[i-1] != '\n' {
614		i++
615	}
616
617	// get working buffer
618	var raw bytes.Buffer
619
620	// put the first line into the working buffer
621	raw.Write(data[blockEnd:i])
622	blockEnd = i
623
624	// process the following lines
625	containsBlankLine := false
626
627gatherLines:
628	for blockEnd < len(data) {
629		i++
630
631		// find the end of this line
632		for i < len(data) && data[i-1] != '\n' {
633			i++
634		}
635
636		// if it is an empty line, guess that it is part of this item
637		// and move on to the next line
638		if p.isEmpty(data[blockEnd:i]) > 0 {
639			containsBlankLine = true
640			blockEnd = i
641			continue
642		}
643
644		n := 0
645		if n = isIndented(data[blockEnd:i], indentSize); n == 0 {
646			// this is the end of the block.
647			// we don't want to include this last line in the index.
648			break gatherLines
649		}
650
651		// if there were blank lines before this one, insert a new one now
652		if containsBlankLine {
653			raw.WriteByte('\n')
654			containsBlankLine = false
655		}
656
657		// get rid of that first tab, write to buffer
658		raw.Write(data[blockEnd+n : i])
659		hasBlock = true
660
661		blockEnd = i
662	}
663
664	if data[blockEnd-1] != '\n' {
665		raw.WriteByte('\n')
666	}
667
668	contents = raw.Bytes()
669
670	return
671}
672
673//
674//
675// Miscellaneous helper functions
676//
677//
678
679// Test if a character is a punctuation symbol.
680// Taken from a private function in regexp in the stdlib.
681func ispunct(c byte) bool {
682	for _, r := range []byte("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~") {
683		if c == r {
684			return true
685		}
686	}
687	return false
688}
689
690// Test if a character is a whitespace character.
691func isspace(c byte) bool {
692	return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'
693}
694
695// Test if a character is letter.
696func isletter(c byte) bool {
697	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
698}
699
700// Test if a character is a letter or a digit.
701// TODO: check when this is looking for ASCII alnum and when it should use unicode
702func isalnum(c byte) bool {
703	return (c >= '0' && c <= '9') || isletter(c)
704}
705
706// Replace tab characters with spaces, aligning to the next TAB_SIZE column.
707// always ends output with a newline
708func expandTabs(out *bytes.Buffer, line []byte, tabSize int) {
709	// first, check for common cases: no tabs, or only tabs at beginning of line
710	i, prefix := 0, 0
711	slowcase := false
712	for i = 0; i < len(line); i++ {
713		if line[i] == '\t' {
714			if prefix == i {
715				prefix++
716			} else {
717				slowcase = true
718				break
719			}
720		}
721	}
722
723	// no need to decode runes if all tabs are at the beginning of the line
724	if !slowcase {
725		for i = 0; i < prefix*tabSize; i++ {
726			out.WriteByte(' ')
727		}
728		out.Write(line[prefix:])
729		return
730	}
731
732	// the slow case: we need to count runes to figure out how
733	// many spaces to insert for each tab
734	column := 0
735	i = 0
736	for i < len(line) {
737		start := i
738		for i < len(line) && line[i] != '\t' {
739			_, size := utf8.DecodeRune(line[i:])
740			i += size
741			column++
742		}
743
744		if i > start {
745			out.Write(line[start:i])
746		}
747
748		if i >= len(line) {
749			break
750		}
751
752		for {
753			out.WriteByte(' ')
754			column++
755			if column%tabSize == 0 {
756				break
757			}
758		}
759
760		i++
761	}
762}
763
764// Find if a line counts as indented or not.
765// Returns number of characters the indent is (0 = not indented).
766func isIndented(data []byte, indentSize int) int {
767	if len(data) == 0 {
768		return 0
769	}
770	if data[0] == '\t' {
771		return 1
772	}
773	if len(data) < indentSize {
774		return 0
775	}
776	for i := 0; i < indentSize; i++ {
777		if data[i] != ' ' {
778			return 0
779		}
780	}
781	return indentSize
782}
783
784// Create a url-safe slug for fragments
785func slugify(in []byte) []byte {
786	if len(in) == 0 {
787		return in
788	}
789	out := make([]byte, 0, len(in))
790	sym := false
791
792	for _, ch := range in {
793		if isalnum(ch) {
794			sym = false
795			out = append(out, ch)
796		} else if sym {
797			continue
798		} else {
799			out = append(out, '-')
800			sym = true
801		}
802	}
803	var a, b int
804	var ch byte
805	for a, ch = range out {
806		if ch != '-' {
807			break
808		}
809	}
810	for b = len(out) - 1; b > 0; b-- {
811		if out[b] != '-' {
812			break
813		}
814	}
815	return out[a : b+1]
816}