doc: Post-process all auto-generated SVG.

This removes the hardcoded foreground color and inherit it from
the browser and/or CSS stylesheet.

This commit only support the foreground colors, future changes could
extend it to support all standard colors.
This commit is contained in:
Emmanuel Lepage Vallee 2021-12-13 09:55:06 -08:00
parent 197ed41c2d
commit 2c18547a6b
2 changed files with 97 additions and 11 deletions

View File

@ -71,7 +71,9 @@ ${LUA_PATH_}")
set(LUA_COV_RUNNER env -u LUA_PATH_5_1 -u LUA_PATH_5_2 -u LUA_PATH_5_3 "LUA_PATH=${LUA_PATH_}" "AWESOME_THEMES_PATH=${TOP_SOURCE_DIR}/themes/" "SOURCE_DIRECTORY=${TOP_SOURCE_DIR}" ${LUA_COV_RUNNER})
# The documentation images directory.
set(RAW_IMAGE_DIR "${CMAKE_BINARY_DIR}/raw_images")
set(IMAGE_DIR "${CMAKE_BINARY_DIR}/doc/images")
file(MAKE_DIRECTORY "${RAW_IMAGE_DIR}")
file(MAKE_DIRECTORY "${IMAGE_DIR}")
# Escape potentially multiline strings to be part of the API doc.
@ -221,6 +223,7 @@ function(run_test test_path namespace escaped_content)
# Get the file name without the extension.
get_filename_component(${test_path} TEST_FILE_NAME NAME)
set(RAW_IMAGE_PATH "${RAW_IMAGE_DIR}/AUTOGEN${namespace}_${TEST_FILE_NAME}")
set(IMAGE_PATH "${IMAGE_DIR}/AUTOGEN${namespace}_${TEST_FILE_NAME}")
# Read the code and turn it into an usage example.
@ -231,12 +234,14 @@ function(run_test test_path namespace escaped_content)
# Does the test generate an output image?
if(tmp_content MATCHES "--DOC_GEN_IMAGE")
set(OUTPUT_RAW_IMAGE_PATH "${RAW_IMAGE_PATH}.svg")
set(OUTPUT_IMAGE_PATH "${IMAGE_PATH}.svg")
escape_string(
"![Usage example](../images/AUTOGEN${namespace}_${TEST_FILE_NAME}.svg)\n"
"${TEST_DOC_CONTENT}" TEST_DOC_CONTENT ""
)
else()
set(OUTPUT_RAW_IMAGE_PATH "")
set(OUTPUT_IMAGE_PATH "")
endif()
@ -294,30 +299,44 @@ function(run_test test_path namespace escaped_content)
STRING(REPLACE "/" "-" TARGET_NAME ${TARGET_NAME})
set(EXAMPLE_DOC_TEST_TARGETS ${EXAMPLE_DOC_TEST_TARGETS} ${TARGET_NAME}
PARENT_SCOPE)
add_custom_target(${TARGET_NAME}
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} ${IMAGE_PATH}
add_custom_target(${TARGET_NAME}_RAW
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} ${RAW_IMAGE_PATH}
VERBATIM)
if(NOT ${OUTPUT_IMAGE_PATH} STREQUAL "")
file(RELATIVE_PATH rel_output "${TOP_SOURCE_DIR}" "${OUTPUT_IMAGE_PATH}")
add_custom_target(${TARGET_NAME}
COMMAND "${TOP_SOURCE_DIR}/tests/examples/_postprocess.lua" ${OUTPUT_RAW_IMAGE_PATH} "${OUTPUT_IMAGE_PATH}"
VERBATIM
DEPENDS ${TARGET_NAME}_RAW "${TOP_SOURCE_DIR}/tests/examples/_postprocess.lua"
)
if(NOT ${OUTPUT_RAW_IMAGE_PATH} STREQUAL "")
file(RELATIVE_PATH rel_output "${TOP_SOURCE_DIR}" "${OUTPUT_RAW_IMAGE_PATH}")
if(tmp_content MATCHES "--DOC_GEN_OUTPUT")
add_custom_command(
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} ${IMAGE_PATH} ${IGNORE_ERRORS}
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} ${RAW_IMAGE_PATH} ${IGNORE_ERRORS}
# COMMENT "Running ${rel_test_path} (via ${rel_template}, generating ${rel_output})"
DEPENDS ${template} ${test_path}
OUTPUT ${OUTPUT_IMAGE_PATH}
OUTPUT ${OUTPUT_RAW_IMAGE_PATH}
OUTPUT ${expected_output_path}
VERBATIM)
else()
add_custom_command(
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} ${IMAGE_PATH} ${IGNORE_ERRORS}
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} ${RAW_IMAGE_PATH} ${IGNORE_ERRORS}
# COMMENT "Running ${rel_test_path} (via ${rel_template}, generating ${rel_output})"
DEPENDS ${template} ${test_path}
OUTPUT ${OUTPUT_IMAGE_PATH}
OUTPUT ${OUTPUT_RAW_IMAGE_PATH}
VERBATIM)
endif()
set(EXAMPLE_DOC_GENERATED_FILES ${OUTPUT_IMAGE_PATH} ${EXAMPLE_DOC_GENERATED_FILES}
PARENT_SCOPE)
add_custom_command(
COMMAND "${TOP_SOURCE_DIR}/tests/examples/_postprocess.lua" ${OUTPUT_RAW_IMAGE_PATH} "${OUTPUT_IMAGE_PATH}"
DEPENDS ${template} ${test_path} ${OUTPUT_RAW_IMAGE_PATH} "${TOP_SOURCE_DIR}/tests/examples/_postprocess.lua"
OUTPUT "${OUTPUT_IMAGE_PATH}"
VERBATIM)
set(EXAMPLE_DOC_GENERATED_FILES "${OUTPUT_IMAGE_PATH}" ${EXAMPLE_DOC_GENERATED_FILES} PARENT_SCOPE)
elseif(tmp_content MATCHES "--DOC_GEN_OUTPUT")
add_custom_command(
COMMAND "${TOP_SOURCE_DIR}/tests/examples/runner.sh" "${expected_output_path}" ${LUA_COV_RUNNER} ${template} ${test_path} "" ${IGNORE_ERRORS}
@ -338,7 +357,8 @@ file(GLOB_RECURSE test_files FOLLOW_SYMLINKS LIST_DIRECTORIES false
# Find and run all test files.
foreach(file ${test_files})
if ((NOT "${file}" MATCHES ".*/shims/.*")
AND (NOT "${file}" MATCHES ".*/template.lua"))
AND (NOT "${file}" MATCHES ".*/template.lua")
AND (NOT "${file}" MATCHES ".*/_.*[.]lua"))
# Get the file name without the extension.
get_filename_component(TEST_FILE_NAME ${file} NAME_WE)

66
tests/examples/_postprocess.lua Executable file
View File

@ -0,0 +1,66 @@
#!/usr/bin/env lua
-- This script takes `.svg` file generated by Cairo or Inkscape and
-- replace hardcoded colors so they can be set using CSS or the
-- web browser itself. This makes the accessibility mode work and
-- allows themes to be created for the documentation.
local input, output = ...
-- The second 24bit is just the 32 bit converted to #010001 and back.
local FOREGROUNDS = {
"rgb[(]0[.]5%%,0%%,0[.]5%%[)];",
"rgb[(]0[.]392157%%,0%%,0[.]392157%%[)];",
"#010001",
}
local CLASSES = {
stroke = "svg_stroke",
fill = "svg_fill"
}
local i, o = io.open(input, "r"), io.open(output, "w")
if (not i) or (not o) then return end
local line, count = i:read("*line"), 0
while line do
-- Deduplicate and concatenate the classes.
local classes = {}
local token_found = {}
local class_str = {}
for _, token in ipairs { "fill", "stroke" } do
for _, color in ipairs(FOREGROUNDS) do
line, count = line:gsub(token .. ":" .. color, token .. ":currentcolor;")
-- Add the CSS class.
if count > 0 then
classes[CLASSES[token]] = true
end
line, count = line:gsub(token .. ":" .. color, token .. ":currentcolor;")
-- Add the CSS class.
if count > 0 and not token_found[token] then
token_found[token] = true
table.insert(classes, CLASSES[token])
end
end
end
for class in pairs(classes) do
table.insert(class_str, class)
end
if #class_str > 0 then
line = line:gsub(' style="', ' class="' .. table.concat(class_str, ",") .. '" style="')
end
o:write(line .. "\n")
line = i:read("*line")
end
o:close()