Debian Codegraph @

support/babel/html.c

raw symbols SCIP
1 /* html.c Treaty of Babel module for HTML files
2 * Written 2020 By Andrew Plotkin
3 *
4 * This file depends on treaty_builder.h
5 *
6 * This file is public domain, but note that any changes to this file
7 * may render it noncompliant with the Treaty of Babel
8 */
9
10 #define FORMAT html
11 #define HOME_PAGE "https://babel.ifarchive.org/"
12 #define FORMAT_EXT ".html"
13 #define NO_METADATA
14 #define NO_COVER
15
16 #include "treaty_builder.h"
17 #include "ifiction.h"
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <strings.h>
21
22 /* Searches case-insensitively for the string str in the story file.
23 The string must be null-terminated. The story file can have nulls
24 anywhere, of course.
25
26 Returns the found position, or -1 for not found.
27 */
28 static int32 find_text_in_file(void *story_file, int32 extent, char *str)
29 {
30 int len = strlen(str);
31 int32 ix;
32
33 if (len == 0) {
34 return -1;
35 }
36
37 for (ix=0; ix<extent-len; ix++) {
38 if (strncasecmp((char *)story_file+ix, str, len) == 0)
39 return ix;
40 }
41
42 return -1;
43 }
44
45 /* Same as the above, except that str and str2 can be separated by any text that does not contain "<" or ">". This is a crude way to locate an HTML tag with an attribute.
46 Kids, don't parse HTML this way.
47 */
48 static int32 find_text_pair_in_file(char *story_file, int32 extent, char *str, char *str2)
49 {
50 int len = strlen(str);
51 int len2 = strlen(str2);
52 int32 ix, jx;
53
54 if (len == 0) {
55 return -1;
56 }
57
58 for (ix=0; ix<extent-len; ix++) {
59 if (strncasecmp(story_file+ix, str, len) == 0) {
60 for (jx=ix+len; jx<extent-len2; jx++) {
61 char ch = *(char *)(story_file+jx);
62 if (ch == '<' || ch == '>')
63 break;
64 if (strncasecmp(story_file+jx, str2, len2) == 0) {
65 return ix;
66 }
67 }
68 }
69 }
70
71 return -1;
72 }
73
74 static int32 find_attribute_value(char *story_file, int32 extent, char *output, int32 output_extent, int32 pos, char* attribute_prefix) {
75 char *starttag = story_file + pos;
76 char *endtag = memchr(starttag, '>', extent-pos);
77 if (endtag) {
78 int32 attrpos = find_text_in_file(starttag, endtag-starttag, attribute_prefix);
79 if (attrpos != -1) {
80 attrpos += strlen(attribute_prefix);
81 char *endattr = memchr(starttag+attrpos, '"', endtag-(starttag+attrpos));
82 if (endattr) {
83 /* Got it. */
84 int32 attrlen = endattr - (starttag+attrpos);
85 ASSERT_OUTPUT_SIZE(attrlen+1);
86 memcpy(output, starttag+attrpos, attrlen);
87 output[attrlen] = '\0';
88 return VALID_STORY_FILE_RV;
89 }
90 }
91 }
92 /* Couldn't find the attribute. */
93 return INVALID_STORY_FILE_RV;
94 }
95
96 static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
97 {
98 int32 ix;
99 int32 pos = find_text_pair_in_file(story_file, extent, "<meta", "property=\"ifiction:ifid\"");
100 if (pos != -1) {
101 return find_attribute_value(story_file, extent, output, output_extent, pos, "content=\"");
102 }
103
104 /* UUID style */
105 ix = find_uuid_ifid_marker(story_file, extent, output, output_extent);
106 if (ix == VALID_STORY_FILE_RV || ix == INVALID_USAGE_RV)
107 return ix;
108
109 /* Twine 2 */
110 pos = find_text_in_file(story_file, extent, "<tw-storydata");
111 if (pos != -1) {
112 return find_attribute_value(story_file, extent, output, output_extent, pos, "ifid=\"");
113 }
114
115 /* Generate IFID from MD5 */
116 ASSERT_OUTPUT_SIZE(8);
117 strcpy(output,"HTML-");
118 return INCOMPLETE_REPLY_RV;
119 }
120
121 static int32 claim_story_file(void *story_file, int32 extent)
122 {
123 if (find_text_in_file(story_file, extent, "<tw-storydata") != -1) {
124 /* Twine 2 or later. See https://github.com/iftechfoundation/twine-specs/blob/master/twine-2-htmloutput-spec.md#story-data */
125 return VALID_STORY_FILE_RV;
126 }
127
128 if (find_text_in_file(story_file, extent, "modifier=\"twee\"") != -1) {
129 /* Twine 1, almost certainly. */
130 return VALID_STORY_FILE_RV;
131 }
132
133 /* every string counts as invalid HTML, but these are pretty definitive */
134 if (find_text_in_file(story_file, extent, "<html") != -1) {
135 return VALID_STORY_FILE_RV;
136 }
137 if (find_text_in_file(story_file, extent, "<!doctype html") != -1) {
138 return VALID_STORY_FILE_RV;
139 }
140 if (find_text_pair_in_file(story_file, extent, "<meta", "property=\"ifiction:ifid\"") != -1) {
141 return VALID_STORY_FILE_RV;
142 }
143 return INVALID_STORY_FILE_RV;
144 }