comparison cmake/FindPandoc.cmake @ 0:3aa05e02d136

Initial import
author David Demelier <markand@malikania.fr>
date Fri, 05 Feb 2016 12:57:50 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3aa05e02d136
1 # FindPandoc
2 # ----------
3 #
4 # Find Pandoc executable, this modules defines:
5 #
6 # Pandoc_EXECUTABLE, where to find pandoc's executable
7 # Pandoc_FOUND, if it is found
8 # Pandoc_VERSION, the version
9 #
10 # This module also defines the following macros:
11 #
12 # pandoc(
13 # SOURCES file1 [file2 ...]
14 # OUTPUT output
15 # [FROM format]
16 # [TO format]
17 # [TARGET target]
18 # [DEPENDS dependency ...]
19 # [ALL]
20 # [TOC]
21 # [STANDALONE]
22 # [MAKE_DIRECTORY]
23 # [TEMPLATE file]
24 # [FILTER filter]
25 # [HEADER header ...]
26 # [FOOTER footer ...]
27 # [BODY body ...]
28 # [VARIABLE var ...]
29 # [METADATA meta ...]
30 # [ARGS argument ...]
31 # [WORKING_DIRECTORY directory]
32 # )
33 #
34 # The sources files are listed in the parameter SOURCES, all files are passed
35 # in the same order they are passed to that variable.
36 #
37 # The OUTPUT file is set with OUTPUT. It is generated only if one of the file
38 # has changed.
39 #
40 # The FROM (-f) and TO (-t) arguments specify respectively the source and
41 # destinations formats.
42 #
43 # If the parameter TARGET is set, then a target named `target` will be added
44 # with the OUTPUT file as the dependency but not listed as sources files.
45 # But the SOURCES files will be added as the target sources in the IDE.
46 #
47 # Optional dependencies can be added to the output command (not the target) with
48 # the DEPENDS parameter.
49 #
50 # If ALL is set and TARGET is also set, the target will be added to the ALL_BUILD.
51 #
52 # If TOC (--toc) is specified, a table of content will be automatically created.
53 #
54 # If STANDALONE (-s) is set, the compilation will assume that it is standalone
55 # and adds the necessary of the output format.
56 #
57 # Optional MAKE_DIRECTORY can be set to create the output directory before
58 # pandoc processes the file (recommended).
59 #
60 # The TEMPLATE parameter can be used to specify the formate template file.
61 #
62 # You can set a filter with the parameter FILTER. The filter will be added to
63 # the output dependencies so you can safely use CMake's targets.
64 #
65 # The HEADER (-H), FOOTER (-A) and BODY (-B) are copied verbatim before, just
66 # after and after the body respectively. They can be set more than once.
67 #
68 # You can pass variables (-V) and metadata (-M) to the parameters VARIABLE
69 # and METADATA, be sure to pass the same syntax as pandoc. (e.g VARIABLE foo=1)
70 #
71 # ARGS is an optional list of additional arguments to pass to pandoc.
72 #
73 # The parameter WORKING_DIRECTORY can be set to change the directory when pandoc
74 # is invoked.
75 #
76
77 find_program(
78 Pandoc_EXECUTABLE
79 NAMES pandoc
80 DOC "Pandoc executable"
81 )
82
83 include(FindPackageHandleStandardArgs)
84 include(CMakeParseArguments)
85
86 # Extract the version
87 if (Pandoc_EXECUTABLE)
88 execute_process(
89 COMMAND ${Pandoc_EXECUTABLE} --version
90 OUTPUT_VARIABLE _pandoc_version_tmp
91 )
92
93 if (_pandoc_version_tmp MATCHES "^pandoc[^ ]* ([0-9]+\\.[0-9]+\\.[0-9]+)")
94 set(Pandoc_VERSION "${CMAKE_MATCH_1}")
95 endif ()
96 endif ()
97
98 find_package_handle_standard_args(
99 Pandoc
100 FOUND_VAR Pandoc_FOUND
101 VERSION_VAR Pandoc_VERSION
102 REQUIRED_VARS Pandoc_EXECUTABLE
103 )
104
105 if (Pandoc_FOUND)
106 function(pandoc)
107 set(options MAKE_DIRECTORY STANDALONE TOC)
108 set(oneValueArgs FILTER FROM TARGET TEMPLATE TO OUTPUT WORKING_DIRECTORY)
109 set(multiValueArgs ARGS FOOTER HEADER METADATA SOURCES VARIABLE)
110
111 #
112 # The following variables will be set in that scope:
113 # _pandoc_arguments - List of all arguments that will passed to pandoc invocation.
114 # _pandoc_depends - List of all dependencies attached to the add_custom_command.
115 # _pandoc_mkdir - The mkdir command if MAKE_DIRECTORY is set
116 # _pandoc_output_base - The base output directory
117 #
118 cmake_parse_arguments(PANDOC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
119
120 #
121 # Output and sources are mandatory
122 #
123 if (NOT PANDOC_OUTPUT)
124 message(FATAL_ERROR "Please define OUTPUT")
125 elseif (NOT PANDOC_SOURCES)
126 message(FATAL_ERROR "Please defines SOURCES")
127 endif ()
128
129 #
130 # Handle the filter with care.
131 #
132 # 1. If it is a target, depend on it and use a generator
133 # expression to get its full path on the disk.
134 # 2. If it is not a target, just use the user provided path.
135 #
136 if (PANDOC_FILTER)
137 # If it is a target, add a dependency so that it is built
138 if (TARGET ${PANDOC_FILTER})
139 list(APPEND _pandoc_arguments --filter "$<TARGET_FILE:${PANDOC_FILTER}>")
140 list(APPEND _pandoc_depends ${PANDOC_FILTER})
141 else ()
142 list(APPEND _pandoc_arguments --filter ${PANDOC_FILTER})
143 endif ()
144 endif ()
145
146 if (PANDOC_TOC)
147 list(APPEND _pandoc_arguments --toc)
148 endif ()
149 if (PANDOC_STANDALONE)
150 list(APPEND _pandoc_arguments -s)
151 endif ()
152 if (PANDOC_FROM)
153 list(APPEND _pandoc_arguments -f ${PANDOC_FROM})
154 endif ()
155 if (PANDOC_TO)
156 list(APPEND _pandoc_arguments -t ${PANDOC_TO})
157 endif ()
158 if (PANDOC_TEMPLATE)
159 list(APPEND _pandoc_arguments --template ${PANDOC_TEMPLATE})
160 list(APPEND _pandoc_depends ${PANDOC_TEMPLATE})
161 endif ()
162
163 # Header, footers and body
164 foreach (h ${PANDOC_HEADER})
165 list(APPEND _pandoc_arguments -H ${h})
166 list(APPEND _pandoc_depends ${h})
167 endforeach ()
168 foreach (b ${PANDOC_BODY})
169 list(APPEND _pandoc_arguments -B ${b})
170 list(APPEND _pandoc_depends ${b})
171 endforeach ()
172 foreach (f ${PANDOC_FOOTER})
173 list(APPEND _pandoc_arguments -A ${f})
174 list(APPEND _pandoc_depends ${f})
175 endforeach ()
176
177 # Variables and metadata
178 foreach (var ${PANDOC_VARIABLE})
179 list(APPEND _pandoc_arguments -V ${var})
180 endforeach ()
181 foreach (meta ${PANDOC_METADATA})
182 list(APPEND _pandoc_arguments -M ${meta})
183 endforeach ()
184
185 # Optional list of arguments
186 foreach (arg ${PANDOC_ARGS})
187 list(APPEND _pandoc_arguments ${arg})
188 endforeach ()
189
190 # Output and sources
191 list(APPEND _pandoc_arguments -o ${PANDOC_OUTPUT})
192
193 #
194 # The following variables are set within the loop:
195 #
196 # _pandoc_input - The absolute path to the input file.
197 # _pandoc_output_base - The base output directory.
198 #
199 foreach (s ${PANDOC_SOURCES})
200 get_filename_component(_pandoc_input ${s} ABSOLUTE)
201 get_filename_component(_pandoc_output_base ${PANDOC_OUTPUT} DIRECTORY)
202 list(APPEND _pandoc_depends ${_pandoc_input})
203 list(APPEND _pandoc_arguments ${_pandoc_input})
204 endforeach ()
205
206 # Create the output directory if requested
207 if (PANDOC_MAKE_DIRECTORY)
208 set(_pandoc_mkdir ${CMAKE_COMMAND} -E make_directory ${_pandoc_output_base})
209 endif ()
210
211 add_custom_command(
212 OUTPUT ${PANDOC_OUTPUT}
213 COMMAND ${_pandoc_mkdir}
214 COMMAND ${Pandoc_EXECUTABLE} ${_pandoc_arguments}
215 DEPENDS ${_pandoc_depends} ${PANDOC_DEPENDS}
216 WORKING_DIRECTORY ${PANDOC_WORKING_DIRECTORY}
217 VERBATIM
218 )
219
220 if (PANDOC_TARGET)
221 add_custom_target(
222 ${PANDOC_TARGET} ${PANDOC_ALL}
223 SOURCES ${_pandoc_depends}
224 DEPENDS ${PANDOC_OUTPUT}
225 WORKING_DIRECTORY ${PANDOC_WORKING_DIRECTORY}
226 )
227 endif ()
228 endfunction()
229 endif ()
230
231 mark_as_advanced(Pandoc_EXECUTABLE)