commit: 032c5c8c5a3e3d6b26b6ab0695ff9977aaa401c7
parent: df229c96d93c0a68054baa5429fcd748aae5b0d1
author: Chris Noxz <chris@noxz.tech>
date: Fri, 6 Sep 2019 18:39:48 +0200
Add support for multiple wiki-services defined in config.h
3 files changed, 34 insertions(+), 4 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -3,14 +3,25 @@
char language[] = "en";
char temp_file[] = "/tmp/__wikidtmp.XXXXXX";
-const char webpage[] = "https://xx.wikipedia.org/w/api.php"
+
+const Service services[] = {
+ { "wikipedia", "https://xx.wikipedia.org/w/api.php"
+ "?format=json"
+ "&action=query"
+ "&prop=extracts"
+ "&exlimit=1"
+ "&explaintext"
+ "&redirects"
+ "&titles="},
+ { "wiktionary", "https://xx.wiktionary.org/w/api.php"
"?format=json"
"&action=query"
"&prop=extracts"
"&exlimit=1"
"&explaintext"
"&redirects"
- "&titles=";
+ "&titles="},
+};
/* cleanup mappings for wikipedia responses
* NOTE: the replacement cannot be greater in size than the entity itself. If
diff --git a/util.h b/util.h
@@ -3,6 +3,11 @@ typedef enum {
true
} bool;
+typedef struct {
+ const char *name;
+ const char *template;
+} Service;
+
typedef struct {
const char *entity;
const char *replacement;
diff --git a/wikid.c b/wikid.c
@@ -37,7 +37,8 @@ void handle_line(char *line);
int print_wiki(char *subject);
/* variables */
-const char *usage = "usage: wikid [-hlrt] <subject>";
+const char *usage = "usage: wikid [-hlrsSt] <subject>";
+const char *webpage = services[0].template;
unsigned total_line_count = 0;
unsigned section_line_count = 0;
bool blank_line = false;
@@ -308,6 +309,7 @@ int main(int argc, char *argv[])
int buffer_size = 80;
char buffer[buffer_size];
int input_size;
+ int h;
char c;
/* validate input from STDIN */
@@ -331,7 +333,7 @@ int main(int argc, char *argv[])
}
if (state == 0) {
- while ((c = getopt(argc, argv, "hl:rt")) != -1) {
+ while ((c = getopt(argc, argv, "hl:rs:St")) != -1) {
switch (c) {
case 'l':
memmove(language, optarg, 2);
@@ -339,6 +341,16 @@ int main(int argc, char *argv[])
case 'r':
global_options |= 1U << 0;
break;
+ case 's':
+ h = atoi(optarg);
+ if (h < 0 || h >= LENGTH(services))
+ h = 0;
+ webpage = services[h].template;
+ break;
+ case 'S':
+ for (h = 0; h < LENGTH(services); h++)
+ fprintf(stderr, "%d: %s\n", h, services[h].name);
+ return 0;
case 't':
global_options |= 1U << 1;
break;
@@ -348,6 +360,8 @@ int main(int argc, char *argv[])
fprintf(stderr, " -h Print this help text and exit\n");
fprintf(stderr, " -l CODE Language code in ISO 639-1 format\n");
fprintf(stderr, " -r Print wiki in raw format\n");
+ fprintf(stderr, " -s ID Specify service ID\n");
+ fprintf(stderr, " -S List services and IDs and exit\n");
fprintf(stderr, " -t Print wiki in text only format\n");
free(input);