Pandoc:将文档标题设置为第一个标题 [英] Pandoc : set document title to first title

查看:351
本文介绍了Pandoc:将文档标题设置为第一个标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在github上创建一个库,因此我使用具有以下结构的Markdown文件:

I'm creating a library on github, so I'm using a Markdown file for that, with the following structure:

# My main title
## My first section
...
## My second section
...

但是不幸的是,当我使用pandoc将该文档转换为乳胶时:

But unfortunately, when I use pandoc to convert this document into latex:

pandoc README.md --number-sections -f markdown -t latex -s -o doc.tex

文档没有任何标题,并且编号从主标题开始:

the document does not have any title, and the numbering starts from the main title:

1. My main title
1.1. My first section
...
1.2. My second section
...

虽然我想要

My main title <=== document title
1. My first section
...
2. My second section
...

我当然可以使用sed将所有##更改为#,并用% Document My main title替换主标题,但对我来说似乎很脏.什么是进行的好方法?

I could of course use sed to change all ## to #, and replace the main title with % Document My main title but it looks quite dirty to me. What is the good way to proceed?

谢谢!

推荐答案

最好使用pandoc过滤器执行此操作.这是一个Lua过滤器,可以满足您的需求.将其存储在文件中promote-headers.lua并使用pandoc --lua-filter=promote-headers.lua调用pandoc.

Best to use a pandoc filter to do this. Here's a Lua filter that does what you need. Store it in a file, e.g. promote-headers.lua and call pandoc with pandoc --lua-filter=promote-headers.lua.

local title

-- Promote all headers by one level. Set title from level 1 headers,
-- unless it has been set before.
function promote_header (header)

  if header.level >= 2 then
    header.level = header.level - 1
    return header
  end

  if not title then
    title = header.content
    return {}
  end

  local msg = '[WARNING] title already set; discarding header "%s"\n'
  io.stderr:write(msg:format(pandoc.utils.stringify(header)))
  return {}
end

return {
  {Meta = function (meta) title = meta.title end}, -- init title
  {Header = promote_header},
  {Meta = function (meta) meta.title = title; return meta end}, -- set title
}

这篇关于Pandoc:将文档标题设置为第一个标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆