簡體   English   中英

Pandoc:在 a.md 文件中,如何修改 YAML 元數據塊並使用 lua 過濾器將修改后的塊放入 markdown 格式

[英]Pandoc: in a .md file, how to modifiy the YAML metadata block and put the modified block in markdown format using a lua filter

我想在將 block-empty.md 文件的 YAML header 中的元數據的某些部分傳遞給模板文件之前對其進行轉換。 也就是說,工作流程是這樣的:

source_YAML_md_file -> lua_filter -> transformed_YAML_md_file -> template -> xml_file

lua_filter 旨在對子問題項(包括 3 個子項:'answer'、'stem' 和 'key')執行洗牌,然后為它們分配列表順序號(子項 'key' 的內容)。

我不知道如何做 lua 過濾器部分。

  1. YAML 示例文件: source_YAML.md
---
category: matching

name: test question

questiontext_header: Test Header

questiontext_body: |
  * Blah, blah, blah.
  * More stuff.

shuffle: T
  
keys: T

subquestion:
- key: 1
  stem: |
    Old:
    
      * MacDonald
        + Had
        + a Farm.   
      * E-I-E-I-O
      
  answer: |
    * And on his farm
    * he had a cow 
  
- key: 2
  stem: |
    With a moo moo here
    
  answer: |
    and a moo moo there
  
- key: 3
  stem: |
    Twinkle, twinkle,
    
  answer: |
    * Little
    * Star 

  
- key: 4
  stem: |
    How I wonder 
    
  answer: |
    what you are! 
---
  1. shuffle_filter.lua文件:
function shuffle(list)
    math.randomseed(os.time())
    for i = #list, 2, -1 do
        local j = math.random(i)
        list[i], list[j] = list[j], list[i]
    end
end

local vars = {}

function get_vars (m)
  for k, v in pairs(m) do 
    print(k, v, #v) 
    if v == m.subquestion and type(v) == 'table' and v.tag == 'MetaList' then
      print('SUBQUESTION')
      for a, b in pairs(v) do
        print('ab___', a, b)
        for c, d in pairs(b) do
          print('cd______', c, d)
        end
      end
      print('shuffling...')
      shuffle(m.subquestion)
      print('modified after shuffling:')
      for a, b in pairs(m.subquestion) do 
        print('ab___', a, b.t, b, #b)
        for c, d in pairs(b) do
          print('__cd____', '', c, d.t, d, #d) 
          for e, f in pairs(d) do
            if f.t == 'Str' then
              print('changing "key" index:')
              print('           ', 'former index:', '', pandoc.utils.stringify(f))
              f = pandoc.MetaInlines(tostring(a))
              print('____ef_____', '', '', e, f.t, f, d[e])
              print('           ', 'current index:', '', pandoc.utils.stringify(f), '\n\n')
            end
          end
        end
      end
    end
  end 
end

-- function replace (el)
--   if vars[el.text] then
--     return pandoc.Span(vars[el.text])
--   else
--     return el
--   end
-- end
-- 
-- return {{Meta = get_vars}, {Str = replace}}
-- return {Meta = get_vars}

這個可怕的代碼是我一直在試驗的,以檢查是否可以改組和更改“關鍵”內容。 似乎它們是,但我的問題是如何將修改后的結果放入 modified.md 文件中。 (“代碼”基於https://pandoc.org/lua-filters.html#replacing-placeholders-with-their-metadata-value ;我使用注釋替換 function 在這里和那里使用 printS 執行“調試” ...過失!)

我知道 -s 獨立選項:

$ pandoc --lua-filter=shuffle_filter.lua -f markdown -t markdown -s  source_YAML.md

但這會呈現原始的 YAML 東西,而不是修改后的東西。

在此先感謝您的幫助。

看來這就是我想要的:

更新shuffle_filter.lua文件:

function shuffle (m)
    -- 1) shuffle subquestions
    math.randomseed(os.time())
    for i = #m.subquestion, 2, -1 do        
        local j = math.random(i)
        m.subquestion[i], m.subquestion[j] = m.subquestion[j], m.subquestion[i]        
    end
    -- 2) update key index
    for i = 1, #m.subquestion, 1 do
        m.subquestion[i].key = pandoc.MetaInlines(tostring(i))
    end
  return m
end

return {{Meta = shuffle}}
  • 將 output 修改后的元數據到標准輸出(或准備 pipe 它,例如,放入模板):

    $ pandoc --lua-filter=shuffle_filter.lua -s -f markdown -t markdown source_YAML.md

  • 要檢查修改后的 AST:

    $ pandoc --lua-filter=shuffle_filter.lua -s -f markdown -t native source_YAML.md

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM