最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

lua - Platform-neutral line ending for io.stderr::write - Stack Overflow

programmeradmin3浏览0评论

I'm writing a Lua filter for Pandoc to deduce the HTML pagetitle from the first h1 heading of the input document. Other than the print() function, io.stderr.write does not append a newline. I found in the answer to NewLine(\n) alternative in Lua? that [[ and ]] can be used to create multi-line string literals, but surprisingly this does not work with strings that contain nothing but a line break.

local pagetitle

function Header(header)
  if not pagetitle and header.level == 1 then
    pagetitle = pandoc.utils.stringify(header)
  end
end

-- capture current line break:
local EOL = [[
]]

function Meta(meta)
  if not meta.pagetitle then
    if pagetitle then
      meta.pagetitle = pagetitle
    else
      io.stderr:write(
        'WARNING: h1 missing, defaulting HTML pagetitle to input filename.', EOL)
      meta.pagetitle = PANDOC_STATE.input_files[1]
    end
    return meta
  end
end

Maybe that's just a Lua bug. On the other hand, it didn't look right from the start, although the idea seemed quite clever at first.

What would you suggest instead?

I'm writing a Lua filter for Pandoc to deduce the HTML pagetitle from the first h1 heading of the input document. Other than the print() function, io.stderr.write does not append a newline. I found in the answer to NewLine(\n) alternative in Lua? that [[ and ]] can be used to create multi-line string literals, but surprisingly this does not work with strings that contain nothing but a line break.

local pagetitle

function Header(header)
  if not pagetitle and header.level == 1 then
    pagetitle = pandoc.utils.stringify(header)
  end
end

-- capture current line break:
local EOL = [[
]]

function Meta(meta)
  if not meta.pagetitle then
    if pagetitle then
      meta.pagetitle = pagetitle
    else
      io.stderr:write(
        'WARNING: h1 missing, defaulting HTML pagetitle to input filename.', EOL)
      meta.pagetitle = PANDOC_STATE.input_files[1]
    end
    return meta
  end
end

Maybe that's just a Lua bug. On the other hand, it didn't look right from the start, although the idea seemed quite clever at first.

What would you suggest instead?

Share edited Mar 14 at 15:07 Wolf asked Mar 14 at 14:57 WolfWolf 10.3k8 gold badges68 silver badges112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Use the string.format() function to add the newline as \n, also make print warnings a function to be future-safe:

local pagetitle

local function print_warning(msg)
  io.stderr:write(string.format('WARNING: %s\n', msg))
end

function Header(header)
  if not pagetitle and header.level == 1 then
    pagetitle = pandoc.utils.stringify(header)
  end
end

function Meta(meta)
  if not meta.pagetitle then
    if pagetitle then
      meta.pagetitle = pagetitle
    else
      print_warning('h1 missing, defaulting HTML pagetitle to input filename.')
      meta.pagetitle = PANDOC_STATE.input_files[1]
    end
    return meta
  end
end

While writing the question, I figured out an answer myself after finding a matching programming idom.

发布评论

评论列表(0)

  1. 暂无评论