# Stripping pandoc version number

**URL:** <https://discourse.cmake.org/t/stripping-pandoc-version-number/11475>\
**Category:** Code\
**Created:** [August 17, 2024, 9:59pm UTC](https://discourse.cmake.org/t/stripping-pandoc-version-number/11475 "2024-08-17T21:59:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramcdona](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ramcdona/32/2439_2.png) [@ramcdona](https://discourse.cmake.org/u/ramcdona)\
**Post date:** [August 17, 2024, 9:59pm UTC](https://discourse.cmake.org/t/stripping-pandoc-version-number/11475/1 "2024-08-17T21:59:46Z")

</div>

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](https://github.com/jeetsukumaran/cmake-pandocology))

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![ramcdona](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ramcdona/32/2439_2.png) [@ramcdona](https://discourse.cmake.org/u/ramcdona)\
**Post date:** [August 18, 2024, 1:10am UTC](https://discourse.cmake.org/t/stripping-pandoc-version-number/11475/2 "2024-08-18T01:10:17Z")

</div>

I was able to come up with this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)\
**Post date:** [August 18, 2024, 12:44pm UTC](https://discourse.cmake.org/t/stripping-pandoc-version-number/11475/3 "2024-08-18T12:44:19Z")

</div>

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_):

```cmake
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()

```
