1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Present2md converts legacy-syntax present files to Markdown-syntax present files.
//
// Usage:
//
// present2md [-w] [file ...]
//
// By default, present2md prints the Markdown-syntax form of each input file to standard output.
// If no input file is listed, standard input is used.
//
// The -w flag causes present2md to update the files in place, overwriting each with its
// Markdown-syntax equivalent.
//
// Examples
//
// present2md your.article
// present2md -w *.article
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"unicode"
"unicode/utf8"
"golang.org/x/tools/present"
)
func usage() {
fmt.Fprintf(os.Stderr, "usage: present2md [-w] [file ...]\n")
os.Exit(2)
}
var (
writeBack = flag.Bool("w", false, "write conversions back to original files")
exitStatus = 0
)
func main() {
log.SetPrefix("present2md: ")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 {
if *writeBack {
log.Fatalf("cannot use -w with standard input")
}
convert(os.Stdin, "stdin", false)
return
}
for _, arg := range args {
f, err := os.Open(arg)
if err != nil {
log.Print(err)
exitStatus = 1
continue
}
err = convert(f, arg, *writeBack)
f.Close()
if err != nil {
log.Print(err)
exitStatus = 1
}
}
os.Exit(exitStatus)
}
// convert reads the data from r, parses it as legacy present,
// and converts it to Markdown-enabled present.
// If any errors occur, the data is reported as coming from file.
// If writeBack is true, the converted version is written back to file.
// If writeBack is false, the converted version is printed to standard output.
func convert(r io.Reader, file string, writeBack bool) error {
data, err := io.ReadAll(r)
if err != nil {
return err
}
if bytes.HasPrefix(data, []byte("# ")) {
return fmt.Errorf("%v: already markdown", file)
}
// Convert all comments before parsing the document.
// The '//' comment is treated as normal text and so
// is passed through the translation unaltered.
data = bytes.Replace(data, []byte("\n#"), []byte("\n//"), -1)
doc, err := present.Parse(bytes.NewReader(data), file, 0)
if err != nil {
return err
}
// Title and Subtitle, Time, Tags.
var md bytes.Buffer
fmt.Fprintf(&md, "# %s\n", doc.Title)
if doc.Subtitle != "" {
fmt.Fprintf(&md, "%s\n", doc.Subtitle)
}
if !doc.Time.IsZero() {
fmt.Fprintf(&md, "%s\n", doc.Time.Format("2 Jan 2006"))
}
if len(doc.Tags) > 0 {
fmt.Fprintf(&md, "Tags: %s\n", strings.Join(doc.Tags, ", "))
}
// Summary, defaulting to first paragraph of section.
// (Summaries must be explicit for Markdown-enabled present,
// and the expectation is that they will be shorter than the
// whole first paragraph. But this is what the blog does today.)
if strings.HasSuffix(file, ".article") && len(doc.Sections) > 0 {
for _, elem := range doc.Sections[0].Elem {
text, ok := elem.(present.Text)
if !ok || text.Pre {
// skip everything but non-text elements
continue
}
fmt.Fprintf(&md, "Summary:")
for i, line := range text.Lines {
fmt.Fprintf(&md, " ")
printStyled(&md, line, i == 0)
}
fmt.Fprintf(&md, "\n")
break
}
}
// Authors
for _, a := range doc.Authors {
fmt.Fprintf(&md, "\n")
for _, elem := range a.Elem {
switch elem := elem.(type) {
default:
// Can only happen if this type switch is incomplete, which is a bug.
log.Fatalf("%s: unexpected author type %T", file, elem)
case present.Text:
for _, line := range elem.Lines {
fmt.Fprintf(&md, "%s\n", markdownEscape(line, true))
}
case present.Link:
fmt.Fprintf(&md, "%s\n", markdownEscape(elem.Label, true))
}
}
}
// Invariant: the output ends in non-blank line now,
// and after printing any piece of the file below,
// the output should still end in a non-blank line.
// If a blank line separator is needed, it should be printed
// before the block that needs separating, not after.
if len(doc.TitleNotes) > 0 {
fmt.Fprintf(&md, "\n")
for _, line := range doc.TitleNotes {
fmt.Fprintf(&md, ": %s\n", line)
}
}
if len(doc.Sections) == 1 && strings.HasSuffix(file, ".article") {
// Blog drops section headers when there is only one section.
// Don't print a title in this case, to make clear that it's being dropped.
fmt.Fprintf(&md, "\n##\n")
printSectionBody(file, 1, &md, doc.Sections[0].Elem)
} else {
for _, s := range doc.Sections {
fmt.Fprintf(&md, "\n")
fmt.Fprintf(&md, "## %s\n", markdownEscape(s.Title, false))
printSectionBody(file, 1, &md, s.Elem)
}
}
if !writeBack {
os.Stdout.Write(md.Bytes())
return nil
}
return os.WriteFile(file, md.Bytes(), 0666)
}
func printSectionBody(file string, depth int, w *bytes.Buffer, elems []present.Elem) {
for _, elem := range elems {
switch elem := elem.(type) {
default:
// Can only happen if this type switch is incomplete, which is a bug.
log.Fatalf("%s: unexpected present element type %T", file, elem)
case present.Text:
fmt.Fprintf(w, "\n")
lines := elem.Lines
for len(lines) > 0 && lines[0] == "" {
lines = lines[1:]
}
if elem.Pre {
for _, line := range strings.Split(strings.TrimRight(elem.Raw, "\n"), "\n") {
if line == "" {
fmt.Fprintf(w, "\n")
} else {
fmt.Fprintf(w, "\t%s\n", line)
}
}
} else {
for _, line := range elem.Lines {
printStyled(w, line, true)
fmt.Fprintf(w, "\n")
}
}
case present.List:
fmt.Fprintf(w, "\n")
for _, item := range elem.Bullet {
fmt.Fprintf(w, " - ")
for i, line := range strings.Split(item, "\n") {
if i > 0 {
fmt.Fprintf(w, " ")
}
printStyled(w, line, false)
fmt.Fprintf(w, "\n")
}
}
case present.Section:
fmt.Fprintf(w, "\n")
sep := " "
if elem.Title == "" {
sep = ""
}
fmt.Fprintf(w, "%s%s%s\n", strings.Repeat("#", depth+2), sep, markdownEscape(elem.Title, false))
printSectionBody(file, depth+1, w, elem.Elem)
case interface{ PresentCmd() string }:
// If there are multiple present commands in a row, don't print a blank line before the second etc.
b := w.Bytes()
sep := "\n"
if len(b) > 0 {
i := bytes.LastIndexByte(b[:len(b)-1], '\n')
if b[i+1] == '.' {
sep = ""
}
}
fmt.Fprintf(w, "%s%s\n", sep, elem.PresentCmd())
}
}
}
func markdownEscape(s string, startLine bool) string {
var b strings.Builder
for i, r := range s {
switch {
case r == '#' && i == 0,
r == '*',
r == '_',
r == '<' && (i == 0 || s[i-1] != ' ') && i+1 < len(s) && s[i+1] != ' ',
r == '[' && strings.Contains(s[i:], "]("):
b.WriteRune('\\')
}
b.WriteRune(r)
}
return b.String()
}
// Copy of ../../present/style.go adjusted to produce Markdown instead of HTML.
/*
Fonts are demarcated by an initial and final char bracketing a
space-delimited word, plus possibly some terminal punctuation.
The chars are
_ for italic
* for bold
` (back quote) for fixed width.
Inner appearances of the char become spaces. For instance,
_this_is_italic_!
becomes
<i>this is italic</i>!
*/
func printStyled(w *bytes.Buffer, text string, startLine bool) {
w.WriteString(font(text, startLine))
}
// font returns s with font indicators turned into HTML font tags.
func font(s string, startLine bool) string {
if !strings.ContainsAny(s, "[`_*") {
return markdownEscape(s, startLine)
}
words := split(s)
var b bytes.Buffer
Word:
for w, word := range words {
words[w] = markdownEscape(word, startLine && w == 0) // for all the continue Word
if len(word) < 2 {
continue Word
}
if link, _ := parseInlineLink(word); link != "" {
words[w] = link
continue Word
}
const marker = "_*`"
// Initial punctuation is OK but must be peeled off.
first := strings.IndexAny(word, marker)
if first == -1 {
continue Word
}
// Opening marker must be at the beginning of the token or else preceded by punctuation.
if first != 0 {
r, _ := utf8.DecodeLastRuneInString(word[:first])
if !unicode.IsPunct(r) {
continue Word
}
}
open, word := markdownEscape(word[:first], startLine && w == 0), word[first:]
char := word[0] // ASCII is OK.
close := ""
switch char {
default:
continue Word
case '_':
open += "_"
close = "_"
case '*':
open += "**"
close = "**"
case '`':
open += "`"
close = "`"
}
// Closing marker must be at the end of the token or else followed by punctuation.
last := strings.LastIndex(word, word[:1])
if last == 0 {
continue Word
}
if last+1 != len(word) {
r, _ := utf8.DecodeRuneInString(word[last+1:])
if !unicode.IsPunct(r) {
continue Word
}
}
head, tail := word[:last+1], word[last+1:]
b.Reset()
var wid int
for i := 1; i < len(head)-1; i += wid {
var r rune
r, wid = utf8.DecodeRuneInString(head[i:])
if r != rune(char) {
// Ordinary character.
b.WriteRune(r)
continue
}
if head[i+1] != char {
// Inner char becomes space.
b.WriteRune(' ')
continue
}
// Doubled char becomes real char.
// Not worth worrying about "_x__".
b.WriteByte(char)
wid++ // Consumed two chars, both ASCII.
}
text := b.String()
if close == "`" {
for strings.Contains(text, close) {
open += "`"
close += "`"
}
} else {
text = markdownEscape(text, false)
}
words[w] = open + text + close + tail
}
return strings.Join(words, "")
}
// split is like strings.Fields but also returns the runs of spaces
// and treats inline links as distinct words.
func split(s string) []string {
var (
words = make([]string, 0, 10)
start = 0
)
// appendWord appends the string s[start:end] to the words slice.
// If the word contains the beginning of a link, the non-link portion
// of the word and the entire link are appended as separate words,
// and the start index is advanced to the end of the link.
appendWord := func(end int) {
if j := strings.Index(s[start:end], "[["); j > -1 {
if _, l := parseInlineLink(s[start+j:]); l > 0 {
// Append portion before link, if any.
if j > 0 {
words = append(words, s[start:start+j])
}
// Append link itself.
words = append(words, s[start+j:start+j+l])
// Advance start index to end of link.
start = start + j + l
return
}
}
// No link; just add the word.
words = append(words, s[start:end])
start = end
}
wasSpace := false
for i, r := range s {
isSpace := unicode.IsSpace(r)
if i > start && isSpace != wasSpace {
appendWord(i)
}
wasSpace = isSpace
}
for start < len(s) {
appendWord(len(s))
}
return words
}
// parseInlineLink parses an inline link at the start of s, and returns
// a rendered Markdown link and the total length of the raw inline link.
// If no inline link is present, it returns all zeroes.
func parseInlineLink(s string) (link string, length int) {
if !strings.HasPrefix(s, "[[") {
return
}
end := strings.Index(s, "]]")
if end == -1 {
return
}
urlEnd := strings.Index(s, "]")
rawURL := s[2:urlEnd]
const badURLChars = `<>"{}|\^[] ` + "`" // per RFC2396 section 2.4.3
if strings.ContainsAny(rawURL, badURLChars) {
return
}
if urlEnd == end {
simpleURL := ""
url, err := url.Parse(rawURL)
if err == nil {
// If the URL is http://foo.com, drop the http://
// In other words, render [[http://golang.org]] as:
// <a href="http://golang.org">golang.org</a>
if strings.HasPrefix(rawURL, url.Scheme+"://") {
simpleURL = strings.TrimPrefix(rawURL, url.Scheme+"://")
} else if strings.HasPrefix(rawURL, url.Scheme+":") {
simpleURL = strings.TrimPrefix(rawURL, url.Scheme+":")
}
}
return renderLink(rawURL, simpleURL), end + 2
}
if s[urlEnd:urlEnd+2] != "][" {
return
}
text := s[urlEnd+2 : end]
return renderLink(rawURL, text), end + 2
}
func renderLink(href, text string) string {
text = font(text, false)
if text == "" {
text = markdownEscape(href, false)
}
return "[" + text + "](" + href + ")"
}
|