-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzimdown.c
410 lines (341 loc) · 10.3 KB
/
zimdown.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/******************************************************************************
* File: zimdown.c
* Author: Brandon Milton
* Website: http://brandonsoft.com
* GitHub: http://github.com/brandonio21
* May 19, 2014
*
* The purpose of zimdown.c is to provide an easy way to convert ZIM files into
* markdown files. Basically, this program simply converts the syntax of each
* markup format so that they can be interchanged.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "strings.h"
#include "symbols.h"
#define FILEOPEN_READ "r"
#define FILEOPEN_WRITE "w"
void convertLine(char *line, char *result);
void convertHeader(char *toConvert, char *result);
void convertItalics(char *toConvert, int *result);
void convertBold(char *toConvert, int *result);
void convertImage(char *toConvert, char *result);
int main(int argc, const char* argv[])
{
/* The first thing we need to do is check the parameters */
/* If the user specified no options */
if (argc <= 1)
{
printf("%s", STR_USAGE);
return EXIT_FAILURE;
}
/* Now, if the user specified the name of a file, do something */
/* for now, we will just assume that they specified a correct filepath */
FILE *readFile = fopen(argv[1], FILEOPEN_READ);
FILE *writeFile = fopen(argv[2], FILEOPEN_WRITE);
if (readFile == NULL)
{
perror(STR_ERR_READFILE);
return EXIT_FAILURE;
}
if (writeFile == NULL)
{
perror(STR_ERR_WRITEFILE);
return EXIT_FAILURE;
}
/* Now we are going to read every line of the first file */
char readFileLine[BUFSIZ];
while (fgets(readFileLine, BUFSIZ, readFile) != NULL)
{
/* While there are still lines to read from the file, we analyze the line */
printf("Read Line: %s", readFileLine);
char lineToWrite[BUFSIZ];
int i = 0;
for (i = 0; i < BUFSIZ; i++)
lineToWrite[i] = '\0';
convertLine(readFileLine, lineToWrite);
/* Now we need to remove the newline, and add two spaces before it */
char *newline;
if ((newline = strchr(lineToWrite, '\n')) != NULL)
{
*newline = ' ';
strcat(lineToWrite, " \n");
}
printf("Converted Line: %s", lineToWrite);
fwrite(lineToWrite, sizeof(char), strlen(lineToWrite), writeFile);
}
/* Now close the files */
fclose(readFile);
fclose(writeFile);
}
void convertLine(char *line, char *result)
{
printf("Line to Convert: %s", line);
int index = 0;
int header = 1;
int image = 1;
int changes = 0;
while (line[index] != '\0')
{
int resultant = 0;
switch (line[index])
{
case ZIM_HEADER :
if (header == 1)
{
convertHeader(line, result);
if (strcmp(line, result) == 0)
{
header = 0;
changes = 1;
break;
}
else
return;
}
case ZIM_IMAGE_OPEN :
if (image == 1)
{
printf("%s\n", "converting img");
convertImage(line, result);
printf("%s\n", "done converting img");
if (strcmp(line, result) == 0)
{
image = 0;
changes = 1;
break;
}
else
return;
}
case ZIM_BOLD :
convertBold(line, &resultant);
strcpy(result, line);
changes = 1;
break;
case ZIM_ITALICS :
convertItalics(line, &resultant);
strcpy(result, line);
if (resultant == 1)
index-=3; /* makeup for lost characters */
changes = 1;
break;
}
++index;
}
if (changes == 0)
strcpy(result, line);
}
void convertItalics(char *toConvert, int *result)
{
/* look for the first pair of slashes */
char *slash = strchr(toConvert, ZIM_ITALICS);
while (slash != NULL && *(slash + 1) != ZIM_ITALICS)
slash = strchr(slash+1, ZIM_ITALICS);
if (slash == NULL)
{
*result = 0;
return;
}
/* now that the first pair of slashes has been found, string at slash+2 */
char *secondSlash = strchr(slash+2, ZIM_ITALICS);
while (secondSlash != NULL && *(secondSlash + 1) != ZIM_ITALICS)
secondSlash = strchr(secondSlash+1, ZIM_ITALICS);
if (secondSlash == NULL)
{
*result = 0;
return;
}
/* replace the characters */
*slash = MARKDOWN_ITALICS;
*(slash+1) = MARKDOWN_ITALICS;
*secondSlash = MARKDOWN_ITALICS;
*(secondSlash+1) = MARKDOWN_ITALICS;
/* Now we need to shift the whitespace out */
char *whitespace;
int whitespaceCount = 0;
/* let's count how many spaces there are */
while (*(whitespace = (slash+2+whitespaceCount)) == ' ' || *whitespace == '\t')
++whitespaceCount;
/* now let's swap it all out */
char *ps = whitespace-1;
while (whitespaceCount > 0)
{
char temp = *(ps - whitespaceCount + 1); /* set temp to first whitespace */
*(ps - whitespaceCount + 1) = *slash; /* swap first space and white */
*(slash) = temp;
slash++;
whitespaceCount--;
}
/* Let's count whitespace before italics things */
whitespaceCount = 0;
while (*(whitespace = secondSlash-1-whitespaceCount) == ' ' ||
*whitespace == '\t')
++whitespaceCount;
/* now let's shift it all out */
ps = whitespace+1;
while (whitespaceCount > 0)
{
char temp = *(ps + whitespaceCount - 1);
*(ps + whitespaceCount - 1) = *(secondSlash+1);
*(secondSlash+1) = temp;
secondSlash--;
whitespaceCount--;
}
/* Now we need to move all other things left */
int index = slash+2-toConvert;
for(index = (slash+2-toConvert); index <= secondSlash-toConvert; index++)
toConvert[index-1] = toConvert[index];
/* Now let's move all the things after the slashes left */
int secondIndex = secondSlash-toConvert+2;
while (toConvert[secondIndex] != '\0')
{
toConvert[secondIndex-2] = toConvert[secondIndex];
secondIndex++;
}
toConvert[secondIndex-2] = '\0'; /* end will null! */
*result = 1; /* We did changes, tell the program */
/* Now let's cleverly recombine the strings */
printf("\nFinal String: %s", toConvert);
}
void convertBold(char *toConvert, int *result)
{
/* look for the first pair of asterisks */
char *star = strchr(toConvert, ZIM_BOLD);
while (star != NULL && *(star + 1) != ZIM_BOLD)
star = strchr(star+1, ZIM_BOLD);
if (star == NULL)
{
*result = 0;
return;
}
/* Now we need to find the second pair of asterisks */
char *secondStar = strchr(star+2, ZIM_BOLD);
while (secondStar != NULL && *(secondStar + 1) != ZIM_BOLD)
secondStar = strchr(star+3, ZIM_BOLD);
if (secondStar == NULL)
{
*result = 0;
return;
}
/* replace the characters */
*star = MARKDOWN_BOLD;
*(star+1) = MARKDOWN_BOLD;
*secondStar = MARKDOWN_BOLD;
*(secondStar+1) = MARKDOWN_BOLD;
/* Markdown bolding must be **cccc** where c is a character. Remove spaces */
char *space;
int spaceCount = 0;
/* Let's count how many spaces there are */
while (*(space = (star+2+spaceCount)) == ' ' || *space == '\t')
++spaceCount;
/* now we need to shift it all left */
char *ps = space-1;
while (spaceCount > 0)
{
char temp = *(ps - spaceCount + 1);
*(ps - spaceCount + 1) = *star;
*star = temp;
star++;
spaceCount--;
}
/* Now let's count how many spaces are to the left of second set */
spaceCount = 0;
while (*(space = (secondStar-1-spaceCount)) == ' ' || *space == '\t')
++spaceCount;
/* Now let's shift it all left */
ps = space+1;
while (spaceCount > 0)
{
char temp = *(ps + spaceCount - 1);
*(ps + spaceCount - 1) = *(secondStar+1);
*(secondStar+1) = temp;
secondStar--;
spaceCount--;
}
}
void convertImage(char *toConvert, char *result)
{
printf("%s\n", "first process");
/* First, we need to find the index of the first curly brace */
char *brace = strchr(toConvert, ZIM_IMAGE_OPEN);
while (brace != NULL && *(brace + 1) != ZIM_IMAGE_OPEN)
brace = strchr(brace+1, ZIM_IMAGE_OPEN);
if (brace == NULL)
{
strcpy(toConvert, result);
return;
}
printf("%s\n", "second process");
/* second, we need to find the index of the closing curly brace */
char *closeBrace = strchr(toConvert, ZIM_IMAGE_CLOSE);
while (closeBrace != NULL && *(closeBrace + 1) != ZIM_IMAGE_CLOSE)
closeBrace = strchr(closeBrace+1, ZIM_IMAGE_CLOSE);
if (closeBrace == NULL)
{
strcpy(toConvert, result);
return;
}
printf("%s\n", "third process");
/* Now let's switch the symbols */
*(closeBrace) = '\0';
*(brace+1) = MARKDOWN_IMAGE_OPEN;
printf("%s\n", "combining");
/* Now we need to combine everything */
strcat(result, "![alt text]");
strcat(result, brace+1);
strcat(result, " \"Title Text\"");
strcat(result, ")");
printf("%s\n", "done");
}
void convertHeader(char *toConvert, char *result)
{
/* First, we need to find the index of the first space */
char *space = strchr(toConvert, ' ');
if (space == NULL) /* There is no space! Not a header! Return! */
{
strcpy(result, toConvert);
return;
}
/* Now replace the first space with a null terminator */
*space = '\0';
space++; /* increase the space pointer */
/* Now let's find the index of the end of the message */
char *secondSpace = strchr(space, ZIM_HEADER);
if (secondSpace == NULL) /* Not a properly formatted header! */
{
/* convert string to what it was before processing and return */
*(space-1) = ' ';
strcpy(result, toConvert);
return;
}
/* Now replace the end of the message space with a null terminator */
*(secondSpace - 1) = '\0';
/* Now we need to see if the lengths of the headers are equal */
int secondCount = 1;
while (*(secondSpace + secondCount) == ZIM_HEADER)
secondCount++;
if ((space-1-toConvert) != secondCount)
{
/* the second header block isn't completing the header, it's not one! */
/* revert the string */
*(space-1) = ' ';
*(secondSpace-1) = ' ';
strcpy(result, toConvert);
return;
}
/* Now that we have our header string and text, convert them to markdown */
/* First, get the header length thing */
int length = ZIM_MD_HEADER_DIFF - (space - toConvert);
/* Now we need to put that many markdown headers in front */
int i = 0;
for (i = 0; i < length; i++)
result[i] = MARKDOWN_HEADER;
/* Insert a space and then insert the message */
result[i] = ' ';
*(secondSpace - 1) = '\n';
*secondSpace = '\0';
strcat(result, space);
}