LuaSnip condition based on asciidoc language metadata

I would like to write LuaSnip snippets that insert single or double quotation marks depending on what language is set in the metadata section of an asciidoc file. For example, let’s say I have the following at the top of the document:

= Document Title
:lang: en-UK
:description: This is a test document.

If I type qtest in insert mode and press Tab, it should expand to ‘’ if it finds :lang: en-UK or “” if it finds :lang: en-US in the metadata section (or at least on one of the first few lines of the file).

Here is what I have managed to write so far:

-- ~/.config/nvim/lua/luasnip-asciidoc-test.lua

local ls = require("luasnip")
local s = ls.snippet
local i = ls.insert_node
local fmt = require("luasnip.extras.fmt").fmt

-- Functions to get language from asciidoctor metadata.
local get_lang_uk = function()
  local lines = vim.api.nvim_buf_get_lines(0, 1, 9, false)
  for _, line in ipairs(lines) do
    return line:match(":lang: en%-UK")
  end
end

local get_lang_us = function()
  local lines = vim.api.nvim_buf_get_lines(0, 1, 9, false)
  for _, line in ipairs(lines) do
    return line:match(":lang: en%-US")
  end
end

ls.add_snippets("asciidoctor", {
  -- Quotation marks for UK English
  s({trig = "qtest", name = "Quotation marks test"}, fmt([[
  `'{}`'
  ]], {
      i(1),
    }),
    { condition = get_lang_uk }
  ),
  -- Quotation marks for US English
  s({trig = "qtest", name = "Quotation marks test"}, fmt([[
  `"{}`"
  ]], {
      i(1),
    }),
    { condition = get_lang_us }
  ),
})

If I source this with :lua require('luasnip-asciidoc-test'), it works, but I have two problems:

It only works for the second line

If I move the line with the lang attribute, there are no matches. I assume this is because I’m using vim.api.nvim_buf_get_lines wrong, but I’m not sure what to change to include lines 2-10.

Having two separate functions is redundant

I have tried using a single function with a table inside it, but then only the snippet for en-UK will be triggered regardless of what is on line 2.

local get_lang = function(lang)
  local lines = vim.api.nvim_buf_get_lines(0, 1, 4, false)
  local table = {
    ['enUK'] = 'en%-UK',
    ['enUS'] = 'en%-US',
  }
  for _, line in ipairs(lines) do
    return line:match(":lang: " .. table[lang])
  end
end

-- Calling the function from the snippet condition
-- ...
    { condition = get_lang('enUK') }
-- ...
    { condition = get_lang('enUS') }

While my post was hidden, I’ve asked around on Matrix, and L3MON4D3, the maintainer of LuaSnip, kindly helped me find a solution to both of my problems.

I rewrote the function as follows:

local function get_lang(lang) return function()
  local lines = vim.api.nvim_buf_get_lines(0, 1, 9, false)
  local table = {
    ['enUK'] = 'en%-UK',
    ['enUS'] = 'en%-US',
  }
  for _, line in ipairs(lines) do
    if line:match(":lang: " .. table[lang]) then return 1 end
  end
end end

Which is again called with condition = get_lang('enUK') or condition = get_lang('enUS').