Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Which one is the YAML standard? Windows file path in metadata #6150

Closed
ma6254 opened this issue Feb 18, 2020 · 1 comment
Closed

Which one is the YAML standard? Windows file path in metadata #6150

ma6254 opened this issue Feb 18, 2020 · 1 comment

Comments

@ma6254
Copy link

ma6254 commented Feb 18, 2020

Thank you for reading this issue, and happy Chinese New Year

Environment

OS: Windows 10
Pandoc Version:

PS C:\Users\mjc\git\FictionDown\release> pandoc -v
pandoc.exe 2.9.2
Compiled with pandoc-types 1.20, texmath 0.12.0.1, skylighting 0.8.3.2
Default user data directory: C:\Users\mjc\AppData\Roaming\pandoc
Copyright (C) 2006-2019 John MacFarlane
Web:  https://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

Input File

This is part of my Markdown file, generated by one of my Golang programs
See this:ma6254/FictionDown#14

---
title: 诡秘之主
description: |-
  蒸汽与机械的浪潮中,谁能触及非凡?历史和黑暗的迷雾里,又是谁在耳语?我从诡秘中醒来,睁眼看见这个世界:
  枪械,大炮,巨舰,飞空艇,差分机;魔药,占卜,诅咒,倒吊人,封印物……光明依旧照耀,神秘从未远离,这是一段“愚者”的传说。
creator: 爱潜水的乌贼
lang: zh-CN
cover-image: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg
---
# 简介
...more...

An error occurred

Try to convert to epub

PS C:\Users\mjc\git\FictionDown\release> pandoc -o a.epub 诡秘之主.md
pandoc.exe: C:_cover_703999991.jpg: openBinaryFile: does not exist (No such file or directory)

If you modify it like this

cover-image: C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg

There is no problem, but modify it like this

cover-image: "C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg"
PS C:\Users\mjc\git\FictionDown\release> pandoc -o a.epub 诡秘之主.md
pandoc.exe: C:_cover_703999991.jpg: openBinaryFile: does not exist (No such file or directory)

The error happened again

In Golang

package main

import (
	"fmt"
	"log"

	"gopkg.in/yaml.v2"
)

var data = `
a: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg
b: C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg
c: "C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg"
`

// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct {
	A string
	B string
	C string
}

func main() {
	t := T{}

	err := yaml.Unmarshal([]byte(data), &t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- t:\n%#v\n\n", t)

	d, err := yaml.Marshal(&t)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- t dump:\n%s\n\n", string(d))

	m := make(map[interface{}]interface{})

	err = yaml.Unmarshal([]byte(data), &m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- m:\n%#v\n\n", m)

	d, err = yaml.Marshal(&m)
	if err != nil {
		log.Fatalf("error: %v", err)
	}
	fmt.Printf("--- m dump:\n%s\n\n", string(d))
}

output:

--- t:
main.T{A:"C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg", B:"C:\\\\Users\\\\mjc\\\\AppData\\\\Local\\\\Temp\\\\book_cover_703999991.jpg", C:"C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg"}

--- t dump:
a: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg
b: C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg
c: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg


--- m:
map[interface {}]interface {}{"a":"C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg", "b":"C:\\\\Users\\\\mjc\\\\AppData\\\\Local\\\\Temp\\\\book_cover_703999991.jpg", "c":"C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg"}

--- m dump:
a: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg
b: C:\\Users\\mjc\\AppData\\Local\\Temp\\book_cover_703999991.jpg
c: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg

So which is the YAML standard, is it escaped or not?
I'm not sure which one is the universal standard

@ma6254 ma6254 changed the title Which is the YAML standard? Windows file path in metadata Which one is the YAML standard? Windows file path in metadata Feb 18, 2020
@jgm
Copy link
Owner

jgm commented Feb 18, 2020

To diagnose this, use pandoc -t native -s. You'll see how pandoc is parsing this metadata field:

% pandoc -t native -s
---
cover-image: C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg
---
("cover-image",MetaInlines [Str "C:",RawInline (Format "tex") "\\Users",RawInline (Format "tex") "\\mjc",RawInline (Format "tex") "\\AppData",RawInline (Format "tex") "\\Local",RawInline (Format "tex") "\\Temp",RawInline (Format "tex") "\\book",Str "_cover_703999991.jpg"])

Remember, pandoc parses these fields as Markdown. And Pandoc's Markdown enables raw tex by default. \Temp, \Book, etc. are being parsed as raw tex components. These are then omitted in non-tex output.

The version with unquoted backslashes works, because the \\ is passed through to the Markdown parser, which recognizes it as an escaped backslash and hence doesn't parse raw tex.

The quoted version doesn't work, because here the YAML parser is interpreting the \\, and by the time pandoc's Markdown parser gets to it it's just a single \.

Hope that clears things up. now, what's the solution?

You could try any of the following;

  1. use the unquoted version with escaped backslashes

  2. use a raw attribute:

cover-image:  `C:\Users\mjc\AppData\Local\Temp\book_cover_703999991.jpg`{=html}

This tells pandoc to treat this field as raw HTML.

  1. disable the raw_tex extension: pandoc -f markdown-raw_tex -t epub.

@jgm jgm closed this as completed Feb 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants