繁体   English   中英

在 pandoc 过滤器中返回 el.content 的文本字符串

[英]Return the text string of el.content in pandoc filter

使用 pandoc 过滤器时,我只想获取el.content的文本,但它返回一个表格!

.md如下(仅用于调试):

[It's so easy!]{color="red"}. Today is Monday.

我想得到字符串It's so easy! 被打印。 所以,我写了代码:

function Span(el)
  color  = el.attributes['color']
  strTxt = el.content
  print(strTxt)
end

但这不是真的 通过使用el.text也一样!

模块pandoc.utils包含一个 function stringify ,它将一个元素转换为一个列表:

function Span(el)
  print(pandoc.utils.stringify(el))
end

这将打印It's so easy! (请注意 pandoc 对撇号的智能处理的效果:结束的大引号取代了直撇号' )。

有关如何使用它们的更多信息,请参阅Lua 过滤器文档

所以我以前从未使用过 Pandoc,所以如果我在这里做错了什么,请多多指教。

我安装了 Pandoc,我创建了一个过滤器。lua 就像你的一样

function Span(el)
  print(el.content)
end

我用你的内容创建了一个 test.md

[It's so easy!]{color="red"}. Today is Monday.

我跑了 pandoc --lua-filter=filter.lua -f markdown test.md

它打印了

table: 00000000078ba480
<p><span color="red">ItÔÇÖs so easy!</span>. Today is Monday.</p>

不管那件事发生了什么' ...

所以我看了看那张桌子

function Span(el)
  for k,v in pairs(el.content) do print(k,v) end
end

哪个印刷

1       table: 0000000007874e90
2       table: 0000000007875010
3       table: 0000000007875050
4       table: 0000000007875090
5       table: 0000000007876190
<p><span color="red">ItÔÇÖs so easy!</span>. Today is Monday.</p>

所以这一定是手册提到的内联列表

让我们看看里面!

function Span(el)
  for i, tbl in ipairs(el.content) do
    print(string.format("Table #%d contains: ", i))
    for k, v in pairs(tbl) do
      print(k,v)
    end
  end
end

哪个打印

Table #1 contains:
text    Itâ?Ts
Table #2 contains:
Table #3 contains:
text    so
Table #4 contains:
Table #5 contains:
text    easy!
<p><span color="red">ItÔÇÖs so easy!</span>. Today is Monday.</p>

因此,该表中的那些表很可能是一些内联对象,并且它们具有包含您要查找的单词的文本属性。

您会发现使用一些循环和打印来检查神秘的表格非常简单。

下面的 lua-filter 将以 md 样式标记的文本着色为[It's so **easy**!]{color="red"}以及纯文本。 按名称和 hex-RGB 指定字体颜色的想法(例如[It's so **easy**!]{color="#5588FF"} )最初是在这里实现的,不是我的。 我刚刚修改了原始的 lua-filter,以便我们也可以应用该过滤器来创建revealjs投影仪幻灯片。

Span = function(span)
  color = span.attributes['color']
  -- if no color attribute, return unchange
  if color == nil then return span end

  -- tranform to <span style="color: red;"></span>
  if FORMAT:match 'html' or FORMAT:match 'revealjs' then
    -- remove color attributes
    span.attributes['color'] = nil
    -- use style attribute instead
    span.attributes['style'] = 'color: ' .. color .. ';'
    -- return full span element
    return span
  elseif FORMAT:match 'latex' or FORMAT:match 'beamer' then
    -- remove color attributes
    span.attributes['color'] = nil
    -- encapsulate in latex code
    if string.sub(color, 1, 1) == "#" and #color == 7 then
      -- TODO: requires xcolor
      local R = tostring(tonumber(string.sub(color, 2, 3), 16))
      local G = tostring(tonumber(string.sub(color, 4, 5), 16))
      local B = tostring(tonumber(string.sub(color, 6, 7), 16))
      table.insert(
        span.content, 1,
        pandoc.RawInline('latex', '\\textcolor[RGB]{'..R..','..G..','..B..'}{')
      )
    elseif string.sub(color, 1, 1) == "#" and #color == 4 then
      -- TODO: requires xcolor
      local R = tostring(tonumber(string.sub(color, 2, 2), 16) * 0x11)
      local G = tostring(tonumber(string.sub(color, 3, 3), 16) * 0x11)
      local B = tostring(tonumber(string.sub(color, 4, 4), 16) * 0x11)
      table.insert(
        span.content, 1,
        pandoc.RawInline('latex', '\\textcolor[RGB]{'..R..','..G..','..B..'}{')
      )
    else
      table.insert(
        span.content, 1,
        pandoc.RawInline('latex', '\\textcolor{'..color..'}{')
      )
    end
    table.insert(
      span.content,
      pandoc.RawInline('latex', '}')
    )
    -- returns only span content
    return span.content
  else
    -- for other format return unchanged
    return span
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM