changeset 617:241583937af0

Docs: recreate correctly files
author David Demelier <markand@malikania.fr>
date Tue, 19 Dec 2017 20:22:31 +0100
parents 4761e7cbb6f7
children 5afc0b3a9ad8
files cmake/function/IrccdBuildHtml.cmake doc/html/CMakeLists.txt plugins/CMakeLists.txt plugins/joke/joke.js
diffstat 4 files changed, 177 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/cmake/function/IrccdBuildHtml.cmake	Mon Dec 18 14:55:43 2017 +0100
+++ b/cmake/function/IrccdBuildHtml.cmake	Tue Dec 19 20:22:31 2017 +0100
@@ -105,15 +105,18 @@
     # Replace CMake variables.
     configure_file(
         ${HTML_SOURCE}
-        ${CMAKE_CURRENT_BINARY_DIR}/${dirname}/${basename}.md
+        ${doc_BINARY_DIR}/${dirname}/${basename}.md
         @ONLY
     )
 
+    set(input ${doc_BINARY_DIR}/${dirname}/${basename}.md)
+    set(output ${doc_BINARY_DIR}/html/${dirname}/${basename}.html)
+
     # Pandoc the file.
     pandoc(
-        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${dirname}/${basename}.html
-        SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${dirname}/${basename}.md
-        DEPENDS ${HTML_SOURCE}
+        OUTPUT ${output}
+        SOURCES ${input}
+        DEPENDS ${HTML_SOURCE} ${input}
         TEMPLATE ${html_SOURCE_DIR}/template.html
         VARIABLE baseurl:${baseurl} ${HTML_VARIABLES}
         FROM markdown
@@ -124,13 +127,13 @@
     # Install the documentation file as component if provided.
     if (HTML_COMPONENT)
         install(
-            FILES ${CMAKE_CURRENT_BINARY_DIR}/${dirname}/${basename}.html
+            FILES ${output}
             COMPONENT ${HTML_COMPONENT}
             DESTINATION ${WITH_DOCDIR}/${dirname}
         )
     endif ()
 
     if (HTML_OUTPUT_VAR)
-        set(${HTML_OUTPUT_VAR} ${CMAKE_CURRENT_BINARY_DIR}/${dirname}/${basename}.html)
+        set(${HTML_OUTPUT_VAR} ${output})
     endif ()
 endmacro ()
--- a/doc/html/CMakeLists.txt	Mon Dec 18 14:55:43 2017 +0100
+++ b/doc/html/CMakeLists.txt	Tue Dec 19 20:22:31 2017 +0100
@@ -170,13 +170,17 @@
         VARIABLES ${VARIABLES}
     )
 
+    list(APPEND SOURCES ${doc_SOURCE_DIR}/src/${fname})
     list(APPEND OUTPUTS ${output})
 endforeach ()
 
 add_custom_target(
     html ALL
     SOURCES
-        ${HTML_SOURCES} ${CSS} ${JS} ${OUTPUTS}
+        ${CSS}
+        ${JS}
+        ${OUTPUTS}
+        ${SOURCES}
         ${html_SOURCE_DIR}/template.html
     COMMAND
         ${CMAKE_COMMAND} -E make_directory ${html_BINARY_DIR}/css
--- a/plugins/CMakeLists.txt	Mon Dec 18 14:55:43 2017 +0100
+++ b/plugins/CMakeLists.txt	Tue Dec 19 20:22:31 2017 +0100
@@ -24,6 +24,7 @@
     auth
     hangman
     history
+    joke
     logger
     plugin
     roulette
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/joke/joke.js	Tue Dec 19 20:22:31 2017 +0100
@@ -0,0 +1,162 @@
+/*
+ * joke.js -- display some jokes
+ *
+ * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+// Plugin information.
+info = {
+    author: "David Demelier <markand@malikania.fr>",
+    license: "ISC",
+    summary: "display some jokes",
+    version: "@IRCCD_VERSION@"
+};
+
+// Modules.
+var File    = Irccd.File;
+var Logger  = Irccd.Logger;
+var Plugin  = Irccd.Plugin;
+var Util    = Irccd.Util;
+
+Plugin.config["max-list-lines"] = 5;
+Plugin.format["error"] = "#{nickname}: no jokes available";
+
+/**
+ * Map of server/channel to list of jokes.
+ *
+ * Jokes consists of array of array of strings, they are printed line-per-line
+ * on the desired server/channel.
+ *
+ * This registry keeps track of jokes that were already printed to avoid sending
+ * the same joke over and over.
+ *
+ * It is implemented like this:
+ *
+ * ```json
+ * {
+ *  "channel1@server1": [
+ *      [
+ *          "line 1 of joke 1"
+ *          "line 2 of joke 1"
+ *      ],
+ *      [
+ *          "line 1 of joke 2"
+ *          "line 2 of joke 2"
+ *      ]
+ *  },
+ *  "channel2@server2": [
+ *      [
+ *          "line 1 of joke 1"
+ *          "line 2 of joke 1"
+ *      ],
+ *      [
+ *          "line 1 of joke 2"
+ *          "line 2 of joke 2"
+ *      ]
+ *  },
+ * };
+ * ```
+ */
+var table = {};
+
+/**
+ * Load the jokes for this server and channel.
+ *
+ * Jokes that are to big to fit into the max-list-lines parameter are discarded.
+ *
+ * \param server the server object
+ * \param channel the channel string
+ * \return a ready to use array of jokes
+ * \throw Error if
+ */
+function load(server, channel)
+{
+    var path = Plugin.config.file
+        ? Plugin.config.file
+        : Plugin.paths.data + "/jokes.json";
+
+    // Allow formatting to select different jokes per server/channel.
+    path = Util.format(path, {
+        server: server.toString(),
+        channel: channel
+    });
+
+    try {
+        var file = new File(path, "r");
+    } catch (e) {
+        throw Error(path + ": " + e.message);
+    }
+
+    var data = JSON.parse(file.read());
+
+    if (!data || !data.length)
+        throw Error(path + ": no jokes found");
+
+    var jokes = data.filter(function (joke) {
+        return joke && joke.length <= Plugin.config["max-list-lines"];
+    });
+
+    if (!jokes)
+        throw Error(path + ": empty jokes");
+
+    return jokes;
+}
+
+function id(server, channel)
+{
+    return channel + "@" + server.toString();
+}
+
+function show(server, channel, joke)
+{
+    for (var l = 0; l < joke.length; ++l)
+        server.message(channel, joke[l]);
+}
+
+function remove(i, index)
+{
+    table[i].splice(index, 1);
+
+    if (table[i].length == 0)
+        delete table[i];
+}
+
+function onCommand(server, origin, channel, message)
+{
+    var i = id(server, channel);
+
+    if (!table[i]) {
+        Logger.debug("reloading for " + i);
+
+        try {
+            table[i] = load(server, channel);
+        } catch (e) {
+            Logger.warning(e.message);
+            server.message(channel, Util.format(Plugin.format.error, {
+                server: server.toString(),
+                channel: channel,
+                origin: origin,
+                nickname: Util.splituser(origin)
+            }));
+
+            return;
+        }
+    }
+
+    var index = Math.floor(Math.random() * table[i].length);
+
+    show(server, channel, table[i][index]);
+    remove(i, index);
+}