Stripping pandoc version number

I am using cmake-pandocology to find pandoc and work with its files (GitHub - jeetsukumaran/cmake-pandocology: Compile documents using Pandoc under the CMake build system)

I need to make sure that the ${PANDOC_EXECUTABLE} version is greater than 3.1.9. At present, pandocology does not parse the version number and set a variable that can be tested.

I can do something like this…

execute_process(COMMAND ${PANDOC_EXECUTABLE} --verbose OUTPUT_VARIABLE PANDOC_VERSION_OUTPUT)

However, the output is multi-line and includes a bunch of extra clutter. I have been unable to come up with a combination of string(FIND, string(SUBSTRING, or string(REGEX that will allow me to set a version number variable that I can test against. Here is a sample of the variable:

pandoc 3.3
Features: +server +lua
Scripting engine: Lua 5.4
User data directory: /Users/ramcdona/.local/share/pandoc
Copyright (C) 2006-2024 John MacFarlane. Web: https://pandoc.org
This is free software see the source for copying conditions. There is no
warranty, not even for merchantability or fitness for a particular purpose.

Thanks for any help obtaining the version number in a form that can be tested. It appears that the pandoc project sometimes uses a three digit version such as 3.1.9. So the solution needs to be able to parse that and set it for comparison.

I was able to come up with this:

execute_process( COMMAND ${PANDOC_EXECUTABLE} --version OUTPUT_VARIABLE PANDOC_VERSION_RAW_OUTPUT )
string( REGEX REPLACE "^pandoc ([^\n]+)\n.*" "\\1" PANDOC_VERSION "${PANDOC_VERSION_RAW_OUTPUT}" )
find_package_handle_standard_args( pandoc REQUIRED_VARS PANDOC_EXECUTABLE PANDOC_VERSION VERSION_VAR PANDOC_VERSION )

Which seems to be doing OK. I would appreciate any tips on this if anyone sees any immediate problems.

Can’t say if there is anything wrong with your method, especially if it works fine.

I myself would do it like this (with some more checks):

execute_process(
    COMMAND pandoc --version
    OUTPUT_VARIABLE PANDOC_VERSION_OUTPUT
    OUTPUT_STRIP_TRAILING_WHITESPACE
    COMMAND_ERROR_IS_FATAL ANY
)
string(REGEX MATCH "^pandoc ([0-9]+(\.[0-9]+)+)" PANDOC_VERSION_MATCH "${PANDOC_VERSION_OUTPUT}")
if(NOT PANDOC_VERSION_MATCH OR CMAKE_MATCH_COUNT EQUAL 0) # no match or no groups
    message(FATAL_ERROR "Couldn't find the version value in pandoc output")
else()
    set(PANDOC_VERSION_VALUE ${CMAKE_MATCH_1}) # 0 is the entire match, 1 is the first group
    message(STATUS "PANDOC_VERSION_VALUE: ${PANDOC_VERSION_VALUE}")
endif()
1 Like