我想meta
向我的输出添加元数据(通过标签和 JSON-LD) ,并在某些地方tex4ht
利用标签内容。title
使用以下 MWE:
\documentclass{scrbook}
\title{Some Book}
\subtitle{A novel}
\author{Some Author}
\makeatletter
\let\mytitle\@title
\let\mysubtitle\@subtitle
\let\myauthor\@author
\let\mydate\@date
\makeatother
\begin{document}
\chapter{Introduction}
\chapter{A great beginning}
\end{document}
内容如下make4ht.cfg
:
\Preamble{xhtml,ext=html,charset="utf-8"}
\Configure{TITLE+}{\mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{chapterTITLE+}{\chaptername{} \thechapter{}: #1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{likechapterTITLE+}{#1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{@HEAD}{\HCode{\Hnewline<meta property="og:title" content="\LikeRef{TITLE+}" />}}
\begin{document}
\EndPreamble
我得到了我想要的title
每一页标签的内容,即如果生成单个 HTML 则为书名(标题、副标题、作者),如果按章节拆分则为章节号和标题前缀。
有没有办法利用这一点,例如og:title
或其他元数据字段?指示的示例使用\LikeRef{TITLE+}
似乎不起作用(空输出)。
为了完整性,我正在编译:
make4ht --format html5 --config make4ht.cfg --utf8 book.tex "2,sec-filename,sections+"
但我还希望“单页版本”能够发挥作用:
make4ht --format html5 --config make4ht.cfg --utf8 book.tex "1,sec-filename,sections+"
答案1
尝试这个配置文件:
\Preamble{xhtml,ext=html,charset="utf-8"}
\Tag{mytitle}{\mytitle: \mysubtitle\space| \myauthor}
\Configure{TITLE+}{\LikeRef{mytitle}}
\Configure{chapterTITLE+}{\chaptername{} \thechapter{}: #1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{likechapterTITLE+}{#1 | \mytitle{}: \mysubtitle{} | \myauthor{}}
\Configure{@HEAD}{\HCode{\Hnewline<meta property="og:title" content="\LikeRef{mytitle}" />}}
\begin{document}
\EndPreamble
重要的变化是我们将自定义标题保存到\Tag{mytitle}
属性中,然后可以使用命令读取该\LikeRef{mytitle}
标题。\LikeRef{TITLE+}
不起作用,因为\Configure{TITLE+}
不使用命令保存属性\Tag
,所以它在中不可用\LikeRef
。但此属性由保存\maketitle
,并使用\@title
作为值,这就是我选择不同名称的原因。
结果如下:
<head><title>Chapter 1: Introduction — Some Book: A novel — Some Author</title>
<meta charset='utf-8' />
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' />
<meta content='width=device-width,initial-scale=1' name='viewport' />
<link href='sample.css' rel='stylesheet' type='text/css' />
<meta content='sample.tex' name='src' />
<meta content='Some Book: A novel | Some Author' property='og:title' /></head>
编辑:
如果您希望标题和 相同og:title
,则可以使用 DOM 过滤器。尝试此文件build.lua
:
local domfilter = require "make4ht-domfilter"
local process = domfilter {
function(dom)
-- copy current page title from <title> to <meta property="og:title">
local head = dom:query_selector("head")[1]
-- find head element for faster processing
if head then
local title
for _, el in ipairs(head:query_selector("title")) do
title = el:get_text()
end
if title then
for _, meta in ipairs(head:query_selector("meta[property='og:title']")) do
meta:set_attribute("content", title)
end
end
end
return dom
end
}
Make:match("html$", process)
编译使用:
make4ht --format html5 --config make4ht.cfg -e build.lua --utf8 book.tex "2,sec-filename,sections+"
结果如下:
<head><title>Chapter 1: Introduction — Some Book: A novel — Some Author</title>
<meta charset='utf-8' />
<meta content='TeX4ht (https://tug.org/tex4ht/)' name='generator' />
<meta content='width=device-width,initial-scale=1' name='viewport' />
<link href='sample.css' rel='stylesheet' type='text/css' />
<meta content='sample.tex' name='src' />
<meta content='Chapter 1: Introduction — Some Book: A novel — Some Author' property='og:title' /></head>