Add a test markdown file. Parse the arguments in main to get the input and output file. Read the input file and pass it to the parser. The test markdown file is successfully parsed!

This commit is contained in:
Jeremy Rand 2021-04-14 23:12:04 -04:00
parent bd1424169d
commit b641d4a508
4 changed files with 112 additions and 4 deletions

View File

@ -51,6 +51,7 @@
9D65330C2626246700105D50 /* md4c.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = md4c.c; sourceTree = "<group>"; };
9DDFC7B026269D23006D6E71 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
9DDFC7B126269D3F006D6E71 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
9DDFC7B42627E081006D6E71 /* test.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = test.md; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@ -92,6 +93,7 @@
9D6532EE2626240800105D50 /* Makefile */,
9D6532F02626240800105D50 /* make */,
9D6532FB2626240800105D50 /* Supporting Files */,
9DDFC7B42627E081006D6E71 /* test.md */,
);
path = md2teach;
sourceTree = "<group>";

View File

@ -58,6 +58,14 @@
argument = "$TARGET_BUILD_DIR/md2teach"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "$PROJECT_DIR/md2teach/test.md"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
argument = "outputfile"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction

View File

@ -8,7 +8,9 @@
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "md4c.h"
@ -58,8 +60,6 @@ MD_PARSER parser = {
NULL // syntax
};
char *testMarkdown = "Hello, world!\n\nMore text\n";
// Implementation
@ -108,10 +108,69 @@ static void debugLogHook(const char * message, void * userdata)
int main(int argc, char * argv[])
{
int result;
static int result;
static char * commandName;
static char * inputFileName;
static FILE * inputFile;
static long inputFileLen;
static char * inputBuffer;
static char * outputFileName;
printf("Stack start: %p\n", &result);
result = md_parse(testMarkdown, strlen(testMarkdown), &parser, NULL);
if (argc != 3) {
fprintf(stderr, "USAGE: %s inputfile outputfile\n", argv[0]);
exit(1);
}
commandName = argv[0];
inputFileName = argv[1];
outputFileName = argv[2];
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
fprintf(stderr, "%s: Unable to open file %s, %s\n", commandName, inputFileName, strerror(errno));
exit(1);
}
if (fseek(inputFile, 0l, SEEK_END) != 0) {
fprintf(stderr, "%s: Unable to seek to the end of file %s, %s\n", commandName, inputFileName, strerror(errno));
fclose(inputFile);
exit(1);
}
inputFileLen = ftell(inputFile);
if (inputFileLen < 0) {
fprintf(stderr, "%s: Unable to get size of file %s, %s\n", commandName, inputFileName, strerror(errno));
fclose(inputFile);
exit(1);
}
inputBuffer = malloc(inputFileLen);
if (inputBuffer == NULL) {
fprintf(stderr, "%s: Unable to allocate %ld bytes for input buffer\n", commandName, inputFileLen);
fclose(inputFile);
exit(1);
}
if (fseek(inputFile, 0l, SEEK_SET) != 0) {
fprintf(stderr, "%s: Unable to seek to the beginning of file %s, %s\n", commandName, inputFileName, strerror(errno));
free(inputBuffer);
fclose(inputFile);
exit(1);
}
if (fread(inputBuffer, 1, inputFileLen, inputFile) != inputFileLen) {
fprintf(stderr, "%s: Unable to read all of file %s, %s\n", commandName, inputFileName, strerror(errno));
free(inputBuffer);
fclose(inputFile);
exit(1);
}
result = md_parse(inputBuffer, inputFileLen, &parser, NULL);
printf("Parser result: %d\n", result);
return 0;

39
md2teach/test.md Normal file
View File

@ -0,0 +1,39 @@
# h1
## h2
### h3
#### h4
##### h5
###### h6
h1
==
h2
--
--------------------
indented code
```
fenced code
```
<tag attr='val' attr2="val2">
> quote
* list item
1. list item
[ref]: /url
paragraph
&copy; &#1234; &#xabcd;
`code`
*emph* **strong** ***strong emph***
_emph_ __strong__ ___strong emph___
[ref] [ref][] [link](/url)
![ref] ![ref][] ![img](/url)
<http://example.com> <doe@example.com>
\\ \* \. \` \