使用pandoc lua过滤器删除HTML文件中的数据属性。

huangapple go评论64阅读模式
英文:

Removing data attributes in HTML file with pandoc lua filter

问题

When compiling a document from Quarto, there are some divs with data- attribute values that I'd like to remove. These have specific names like data-output_encrypt:

<div class="cell" data-output_encrypt="key1">
...
</div>

Based on examples I've seen in the docs, etc, I'm imagining the lua filter will look something like this:

if FORMAT:match 'html' then
  function Div (elem)
    elem.attributes.data-output_encrypt = nil
    return elem
  end
end

...but this leads to a syntax error:

ERROR: remove_data.lua:3: syntax error near '-'

I'm guessing because attribute names can't contain hyphens (because they are table keys?). I know I'm on the right track because I can set arbitrary attributes without hyphens (e.g., "data"); I'm just not sure how to deal with the hyphen.

How can I modify/delete the data-output_encrypt attribute?

英文:

When compiling a document from Quarto, there are some divs with data- attribute values that I'd like to remove. These have specific names like data-output_encrypt:

<div class="cell" data-output_encrypt="key1">
...
</div>

Based on examples I've seen in the docs, etc, I'm imagining the lua filter will look something like this:

if FORMAT:match 'html' then
  function Div (elem)
    elem.attributes.data-output_encrypt = nil
    return elem
  end
end

...but this leads to a syntax error:

ERROR: remove_data.lua:3: syntax error near '-'

I'm guessing because attribute names can't contain hyphens (because they are table keys?). I know I'm on the right track because I can set arbitrary attributes without hyphens (e.g., "data"); I'm just not sure how to deal with the hyphen.

How can I modify/delete the data-output_encrypt attribute?

答案1

得分: 2

以下是翻译好的内容:

首先,在将 pandoc Div 中的属性转换为 HTML div 标签属性时,属性名称会以 data- 作为前缀。因此,在 lua 过滤器中访问它们时,您只需使用 data- 后面的部分作为键进行表索引。

其次,elem.attributes 实际上是一个 lua 表,其中属性名称作为键,与表键对应的属性值作为值。要访问一个值,您可以使用 table[key] 语法,其中 key 可以是数字、字符串或除了 nilNaN 之外的任何值,或者如果键是字符串常量,您可以使用 table.键,但这仅在字符串由下划线、字母和数字组成且不以数字开头时才有效。(点击此处查看带有示例的详细信息)

因此,以下任何一种方法都可以工作:

elem.attributes.output_encrypt = nil
-- 或者
elem.attributes["output_encrypt"] = nil

然而,我更喜欢第二种方法。因为通常 HTML div 属性都以 kebab-case(用短划线分隔)编写(参见此处:https://stackoverflow.com/a/49823329/10858321),在这种情况下,您必须使用第二种方法。

英文:

There are two things to note here.

Firstly, attributes name in pandoc Div are prefixed with data- when converted into HTML div tag attributes. So to access them with lua filter you do the table indexing by using only the part after data- as a key.

Secondly, elem.attributes is actually a lua table with attribute names as keys and attribute values as values corresponding to the table keys. And to access a value, you can either use table[key] syntax, where key could be numeric, string or any value except nil or NaN, or if the key is a string constant, you can use it as table. Key where this is only valid if the string consists of underscores, letters, and numbers, but shouldn't start with a number. (See here for details with example)

So either of the below should work,

elem.attributes.output_encrypt = nil
-- or
elem.attributes["output_encrypt"] = nil

However, I prefer the second approach. Because conventionally HTML div attributes are written in kebab-case (separated with dash) most of the time, in which case you must need to follow the second approach.

huangapple
  • 本文由 发表于 2023年6月22日 01:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定