| 1 |
--[[ |
| 2 |
-- This module is part of the LuaHTTP package |
| 3 |
-- The purpose of this module is to correctly display the data reveived from the fetch module. |
| 4 |
]] |
| 5 |
|
| 6 |
local moduleName = display |
| 7 |
local M = {} |
| 8 |
|
| 9 |
---------- Dependencies ------------------------ |
| 10 |
local fetch = require("luahttp-fetch") |
| 11 |
|
| 12 |
---------- Local variables --------------------- |
| 13 |
local tmp_image_counter = 0 -- Counter for image names |
| 14 |
|
| 15 |
---------- Helper functions -------------------- |
| 16 |
|
| 17 |
--- Displays an image using LuaTeX img.write function. |
| 18 |
-- The image has to be saved first in order to be written to the PDF-Document using LuaTeX. |
| 19 |
-- @param data image data |
| 20 |
-- @param width optional width in cm |
| 21 |
-- @param height optional height in cm |
| 22 |
-- @see search_and_escape |
| 23 |
local function display_image(data, width, height) |
| 24 |
local tmp_image_name = '/tmp/tmp_image' -- filename of image saved temporarly |
| 25 |
tmp_image_name = tmp_image_name .. tmp_image_counter |
| 26 |
|
| 27 |
local width = width or nil |
| 28 |
local height = height or nil |
| 29 |
local f = assert(io.open(tmp_image_name, 'wb')) |
| 30 |
f:write(data) |
| 31 |
f:close() |
| 32 |
|
| 33 |
-- LuaTeX does not provide built-in image scaling functions |
| 34 |
local image = img.new({filename = tmp_image_name, width = width, height = height}) |
| 35 |
if image then |
| 36 |
img.write(image) |
| 37 |
tmp_image_counter = tmp_image_counter + 1 |
| 38 |
end |
| 39 |
end |
| 40 |
|
| 41 |
--- Prompts the user to display an image. |
| 42 |
-- If an image-URL is detected the user is asked to display the image or the plain URL. |
| 43 |
-- @see is_image_url |
| 44 |
local function prompt_user() |
| 45 |
while true do |
| 46 |
print("Do you want to display the image? (y/n)") |
| 47 |
local answer = io.read() |
| 48 |
|
| 49 |
if answer == 'y' then |
| 50 |
return true |
| 51 |
elseif answer == 'n' then |
| 52 |
return false |
| 53 |
else |
| 54 |
print("Invalid answer. Please enter 'y' or 'n'.") |
| 55 |
end |
| 56 |
end |
| 57 |
end |
| 58 |
|
| 59 |
--- Searches the given URL for image extensions. |
| 60 |
-- @param url some URL |
| 61 |
-- @return true if an image extension was found, false otherwise |
| 62 |
-- @see search_and_escape |
| 63 |
local function is_image_url(url) |
| 64 |
local image_extensions = { "jpg", "jpeg", "png", "gif" } |
| 65 |
for _, ext in ipairs(image_extensions) do |
| 66 |
if string.match(url, "%." .. ext) then |
| 67 |
return true |
| 68 |
end |
| 69 |
end |
| 70 |
return false |
| 71 |
end |
| 72 |
|
| 73 |
--- Searches the given value for special characters that cause problems in LaTeX-Documents. |
| 74 |
-- @param value single value of a table |
| 75 |
-- @return if no special characters where found the value is retured unchanged, |
| 76 |
-- if special characters where found the escaped value is returned, |
| 77 |
-- if an image-URL is detected and the user chooses to display that image nil is returned |
| 78 |
-- @see is_image_url, prompt_user |
| 79 |
local function search_and_escape(value) |
| 80 |
local value = tostring(value) |
| 81 |
if string.find(value, "^http") then |
| 82 |
if is_image_url(value) then |
| 83 |
print("\nLooks like this URL leads to an image: " .. value) |
| 84 |
if prompt_user() then |
| 85 |
local body = fetch.image(value) |
| 86 |
display_image(body, "5cm") |
| 87 |
return nil |
| 88 |
else |
| 89 |
print("\nEscaping URL: " .. value) |
| 90 |
value = [[\url{]] .. value .. [[}]] |
| 91 |
end |
| 92 |
else |
| 93 |
print("\nEscaping URL: " .. value) |
| 94 |
value = [[\url{]] .. value .. [[}]] |
| 95 |
end |
| 96 |
else |
| 97 |
local latex_special_chars = '([%%$%{%}&%#_%^%~])' |
| 98 |
value = value:gsub(latex_special_chars, "\\%1") |
| 99 |
end |
| 100 |
return value |
| 101 |
end |
| 102 |
|
| 103 |
--- Prints a table to stdout. |
| 104 |
-- @param t tagle to print |
| 105 |
-- @param indent optional string used for indents |
| 106 |
local function print_table(t, indent) |
| 107 |
indent = indent or "" |
| 108 |
for k, v in pairs(t) do |
| 109 |
if type(v) == "table" then |
| 110 |
print(indent .. k .. ":") |
| 111 |
print_table(v, indent .. " ") |
| 112 |
else |
| 113 |
print(indent .. k .. ": " .. tostring(v)) |
| 114 |
end |
| 115 |
end |
| 116 |
end |
| 117 |
|
| 118 |
--- Converts a table to text which can be written to the PDF-Document |
| 119 |
-- The values of the table are first searched for special characters. |
| 120 |
-- @param tbl table to be converted |
| 121 |
-- @return text |
| 122 |
-- @see search_and_escape |
| 123 |
local function table_to_text(tbl) |
| 124 |
local results = {} |
| 125 |
for k, v in pairs(tbl) do |
| 126 |
if type(v) == "table" then |
| 127 |
table.insert(results, table_to_text(v)) |
| 128 |
else |
| 129 |
v = search_and_escape(v) |
| 130 |
if v then |
| 131 |
table.insert(results, v .. " \\\\ ") |
| 132 |
end |
| 133 |
end |
| 134 |
end |
| 135 |
return table.concat(results, " \\ ") |
| 136 |
end |
| 137 |
|
| 138 |
--- Check if a table contains a certain value. |
| 139 |
-- @param table input table |
| 140 |
-- @param target_value value to be searched |
| 141 |
-- @return true if value was found, false otherwise |
| 142 |
local function table_contains(table, target_value) |
| 143 |
for _, value in pairs(table) do |
| 144 |
if type(value) == "table" then |
| 145 |
table_contains(value, target_value) |
| 146 |
elseif value == target_value then |
| 147 |
return true |
| 148 |
end |
| 149 |
end |
| 150 |
return false |
| 151 |
end |
| 152 |
|
| 153 |
--- Filter out table entries that are not in the provided target keys. |
| 154 |
-- @param input_table |
| 155 |
-- @param target_keys array of target keys |
| 156 |
-- @param results used for recursion |
| 157 |
-- @return return a new table containing only the target keys and their values |
| 158 |
local function filter_table(input_table, target_keys, results) |
| 159 |
local results = results or {} |
| 160 |
|
| 161 |
for _, target_key in ipairs(target_keys) do |
| 162 |
for key, value in pairs(input_table) do |
| 163 |
if type(value) == "table" then |
| 164 |
filter_table(value, target_keys, results) |
| 165 |
elseif tostring(key) == target_key then |
| 166 |
if not table_contains(results, value) then |
| 167 |
table.insert(results, value) |
| 168 |
break |
| 169 |
end |
| 170 |
end |
| 171 |
end |
| 172 |
end |
| 173 |
return results |
| 174 |
end |
| 175 |
|
| 176 |
--- Converts a string containing a comma seperated list of elements to an array (ipairs). |
| 177 |
-- @param str input string |
| 178 |
-- @return table containing the elements as values |
| 179 |
local function string_to_ipairs(str) |
| 180 |
local t = {} |
| 181 |
for value in string.gmatch(str, "([^,]+)") do |
| 182 |
table.insert(t, value) |
| 183 |
end |
| 184 |
return t |
| 185 |
end |
| 186 |
|
| 187 |
---------- Module functions -------------------- |
| 188 |
|
| 189 |
--- Reads the contents of a JSON-file, filters the response and prints the result to the PDF-Document. |
| 190 |
-- @param json_file_path path to the JSON-file |
| 191 |
-- @param keys optional keys to filter out the relevant values from the response |
| 192 |
function M.json_using_file(json_file_path, keys) |
| 193 |
local data = fetch.json_using_file(json_file_path) |
| 194 |
print_table(data) |
| 195 |
if keys then |
| 196 |
local keys = string_to_ipairs(tostring(keys)) |
| 197 |
local values = filter_table(data, keys) |
| 198 |
tex.sprint(table_to_text(values)) |
| 199 |
else |
| 200 |
tex.sprint(table_to_text(data)) |
| 201 |
end |
| 202 |
end |
| 203 |
|
| 204 |
--- Prints the response filtered by the keys to the PDF-Document. |
| 205 |
-- @param url URL of the API |
| 206 |
-- @param keys optional keys to filter out the relevant values from the response |
| 207 |
function M.json(url, keys) |
| 208 |
local data = fetch.json(tostring(url)) |
| 209 |
print_table(data) |
| 210 |
if keys then |
| 211 |
local keys = string_to_ipairs(tostring(keys)) |
| 212 |
local values = filter_table(data, keys) |
| 213 |
tex.sprint(table_to_text(values)) |
| 214 |
else |
| 215 |
tex.sprint(table_to_text(data)) |
| 216 |
end |
| 217 |
end |
| 218 |
|
| 219 |
--- Print an image to the PDF-Document. |
| 220 |
-- @param url URL of the image |
| 221 |
-- @param width optional width in cm |
| 222 |
-- @param height optional height in cm |
| 223 |
function M.image(url, width, height) |
| 224 |
local data = fetch.image(tostring(url)) |
| 225 |
display_image(data, width, height) |
| 226 |
end |
| 227 |
|
| 228 |
--- Print values from an rss-feed to the PDF-Document. |
| 229 |
-- @param url URL of the feed |
| 230 |
-- @param limit limits the amount of entries that get printed to the PDF-Document |
| 231 |
-- @param feed_info_keys keys used to filter the feed information |
| 232 |
-- @param entry_keys keys used to filter the feed entries |
| 233 |
function M.rss(url, limit, feed_info_keys, entry_keys) |
| 234 |
local data = fetch.rss(tostring(url)) |
| 235 |
|
| 236 |
if feed_info_keys then |
| 237 |
local feed_info_keys = string_to_ipairs(tostring(feed_info_keys)) |
| 238 |
local feed = data.feed |
| 239 |
local feed_info_filtered = filter_table(feed, feed_info_keys) |
| 240 |
|
| 241 |
tex.sprint(table_to_text(feed_info_filtered)) |
| 242 |
end |
| 243 |
|
| 244 |
local entries = {} |
| 245 |
|
| 246 |
for i = 1, limit do |
| 247 |
if data.entries[i] then |
| 248 |
table.insert(entries, data.entries[i]) |
| 249 |
end |
| 250 |
end |
| 251 |
|
| 252 |
if entry_keys then |
| 253 |
local entry_keys = string_to_ipairs(tostring(entry_keys)) |
| 254 |
local entries_filtered = filter_table(entries, entry_keys) |
| 255 |
|
| 256 |
print_table(entries_filtered) |
| 257 |
tex.sprint(table_to_text(entries_filtered)) |
| 258 |
else |
| 259 |
tex.sprint(table_to_text(entries)) |
| 260 |
end |
| 261 |
end |
| 262 |
|
| 263 |
--- Print the reponse from a request using query parameters to the PDF-Document. |
| 264 |
-- @param url URL of the API |
| 265 |
-- @param keys keys to filter out the relevant values |
| 266 |
-- @param ... multiple optional query parameters used in the request |
| 267 |
function M.json_using_query(url, keys, ...) |
| 268 |
local query_parameters = { ... } |
| 269 |
local data = fetch.json_using_query(url, query_parameters) |
| 270 |
|
| 271 |
print_table(data) |
| 272 |
|
| 273 |
local keys = string_to_ipairs(tostring(keys)) |
| 274 |
local values = filter_table(data, keys) |
| 275 |
tex.sprint(table_to_text(values)) |
| 276 |
end |
| 277 |
|
| 278 |
return M |