示例#1
0
func WriteTOC(entry *ditaconvert.Entry, filename string) {
	os.MkdirAll(filepath.Dir(filename), 0755)
	out, err := os.Create(filename)
	if err != nil {
		panic(err)
	}
	defer out.Close()

	fmt.Fprint(out, `<link rel="stylesheet" href="/style.css">`)
	fmt.Fprint(out, `<base target="dynamic">`)

	var PrintEntry func(entry *ditaconvert.Entry)
	PrintEntry = func(entry *ditaconvert.Entry) {
		if !entry.TOC {
			return
		}
		if entry.Topic == nil {
			fmt.Fprintf(out, `<li>%s`, html.EscapeString(entry.Title))
		} else {
			newpath := ReplaceExt(entry.Topic.Path, ".html")
			fmt.Fprintf(out, `<li><a href="/%s">%s</a>`, html.NormalizeURL(newpath), entry.Title)
		}

		if len(entry.Children) > 0 {
			fmt.Fprint(out, `<ul>`)
			for _, child := range entry.Children {
				PrintEntry(child)
			}
			fmt.Fprint(out, `</ul>`)
		}
		fmt.Fprint(out, "</li>")
	}
	PrintEntry(entry)
}
示例#2
0
func WriteTopic(index *ditaconvert.Index, topic *ditaconvert.Topic, filename string) {
	os.MkdirAll(filepath.Dir(filename), 0755)
	file, err := os.Create(filename)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	out := bufio.NewWriter(file)
	defer out.Flush()

	conversion := ditaconvert.NewConversion(index, topic)
	if err := conversion.Run(); err != nil || len(conversion.Errors) > 0 {
		fmt.Printf("[%s] %s: %v\n", topic.Path, topic.Title, err)
		for _, err := range conversion.Errors {
			fmt.Printf("\t%v\n", err)
		}
		if err != nil {
			return
		}
	}

	fmt.Fprint(out, "<!--INGREDIENTS:\n")
	fmt.Fprint(out, "Keywords=")
	for i, key := range topic.Original.Prolog.Keywords.Terms() {
		if i > 0 {
			fmt.Fprint(out, ",")
		}
		fmt.Fprint(out, key)
	}
	for _, meta := range topic.Original.Prolog.OtherMeta {
		fmt.Fprintf(out, "%s=%s\n", html.EscapeString(meta.Name), html.EscapeString(meta.Content))
	}
	fmt.Fprint(out, "-->\n")
	fmt.Fprint(out, `<body id="`+topic.Original.ID+`">`)
	fmt.Fprint(out, `<h3>`+html.EscapeString(topic.Title)+`</h3>`)
	fmt.Fprint(out, `<div>`)
	fmt.Fprint(out, conversion.Output.String())
	fmt.Fprint(out, `</div>`)
	fmt.Fprint(out, RelatedLinksAsHTML(conversion))
	fmt.Fprint(out, `</body>`)
}