1# - Convert markdown source files to HTML as a custom target
2#
3#  include(UseMarkdown)
4#  add_markdown_target(<target_name> <directory to copy to> <markdownfile> [<markdownfile>...] [RENAME <newname>])
5#    Relative paths for the destination directory are considered with
6#    with respect to CMAKE_CURRENT_BINARY_DIR. The RENAME argument is only
7#    valid with a single markdown file as input.
8#
9#
10#  install_markdown_target(<target_name> [extra arguments to INSTALL(FILES ...) ])
11#
12#
13# Requires CMake 2.6 or newer (uses the 'function' command)
14#
15# Original Author:
16# 2011 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
17# http://academic.cleardefinition.com
18# Iowa State University HCI Graduate Program/VRAC
19#
20# Copyright Iowa State University 2011-2012.
21# Distributed under the Boost Software License, Version 1.0.
22# (See accompanying file LICENSE_1_0.txt or copy at
23# http://www.boost.org/LICENSE_1_0.txt)
24
25if(__add_markdown_target)
26	return()
27endif()
28set(__add_markdown_target YES)
29
30define_property(TARGET
31	PROPERTY
32	MARKDOWN_TARGET_OUTPUTS
33	BRIEF_DOCS
34	"Markdown target outputs"
35	FULL_DOCS
36	"Output files of a target created by add_markdown_target")
37
38function(add_markdown_target _target _dest)
39
40	if(NOT ARGN)
41		message(WARNING
42			"In add_markdown_target call for target ${_target}, no source files were specified!")
43		return()
44	endif()
45
46	find_package(Markdown QUIET)
47	if(NOT MARKDOWN_EXECUTABLE)
48		message(FATAL_ERROR "Can't find a markdown conversion tool!")
49	endif()
50
51	set(NEW_NAME)
52	list(FIND ARGN "RENAME" _renameloc)
53	if(_renameloc GREATER -1)
54		list(LENGTH ARGN _len)
55		if(NOT _len EQUAL 3)
56			message(FATAL_ERROR
57				"Specifying RENAME requires 1 input file and 1 output name!")
58		endif()
59		list(GET ARGN 2 NEW_NAME)
60		list(GET ARGN 0 ARGN)
61	endif()
62
63	set(ALLFILES)
64	set(SOURCES)
65	foreach(fn ${ARGN})
66		# Produce an absolute path to the input file
67		if(IS_ABSOLUTE "${fn}")
68			get_filename_component(fullpath "${fn}" ABSOLUTE)
69			get_filename_component(fn "${fn}" NAME)
70		else()
71			get_filename_component(fullpath
72				"${CMAKE_CURRENT_SOURCE_DIR}/${fn}"
73				ABSOLUTE)
74		endif()
75		get_filename_component(fn_noext "${fn}" NAME_WE)
76
77		# Clean up output file name
78		if(NEW_NAME)
79			get_filename_component(absout "${_dest}/${NEW_NAME}" ABSOLUTE)
80		else()
81			get_filename_component(absout "${_dest}/${fn_noext}.html" ABSOLUTE)
82		endif()
83
84		add_custom_command(OUTPUT "${absout}"
85			COMMAND
86			${CMAKE_COMMAND}
87			ARGS -E make_directory "${_dest}"
88			COMMAND
89			${MARKDOWN_EXECUTABLE}
90			ARGS "${fullpath}" > "${absout}"
91			MAIN_DEPENDENCY "${fullpath}"
92			VERBATIM
93			COMMENT "Converting Markdown ${fn} to HTML in ${absout}...")
94		list(APPEND SOURCES "${fullpath}")
95		list(APPEND ALLFILES "${absout}")
96	endforeach()
97
98	# Custom target depending on all the file copy commands
99	add_custom_target(${_target}
100		ALL
101		SOURCES ${SOURCES}
102		DEPENDS ${ALLFILES})
103	set_property(TARGET ${_target} PROPERTY MARKDOWN_TARGET_OUTPUTS "${ALLFILES}")
104endfunction()
105
106function(install_markdown_target _target)
107	get_target_property(_mtoutputs ${_target} MARKDOWN_TARGET_OUTPUTS)
108	if(NOT _mtoutputs)
109		message(WARNING
110			"install_markdown_target called on a target not created with add_markdown_target!")
111		return()
112	endif()
113
114	# Forward the call to install
115	install(FILES ${_mtoutputs} ${ARGN})
116endfunction()
117