Skip to content

Commit 3d45bfa

Browse files
committed
Use of the RAW markdown submission method :) And it works! =D
1 parent a5630cf commit 3d45bfa

File tree

4 files changed

+56
-98
lines changed

4 files changed

+56
-98
lines changed

headers/ghfmd.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010

11-
#define API_REQUEST_URL "https://api.github.com/markdown"
11+
#define API_REQUEST_URL "https://api.github.com/markdown/raw"
1212
#define API_REQUEST_PORT 443
1313
#define JSON_SQUELETON_SIZE 32
1414

@@ -17,18 +17,6 @@
1717

1818

1919

20-
/**
21-
* Return the JSON expected by the REST API as a string, ready to be sent over
22-
* the network
23-
*
24-
* @param text The markdown formated text submited
25-
*
26-
* @return The JSON expected by the REST API. It returns NULL if no memory
27-
* could be allocated for the result.
28-
*/
29-
char* get_json(char* text);
30-
31-
3220

3321
/**
3422
* Convert a mardown formated content into its corresponding HTML content.

main.c

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,61 @@
1+
/**
2+
* This program reads the file given as first parameter which is supposed to be
3+
* a markdown-formated text file. It reads this file and returns its html
4+
* equivalent to the stdout
5+
*/
6+
7+
18
#include "headers/ghfmd.h"
29

10+
#define MAX_SIZE 12288
11+
312

413
int main(int argc, char** argv) {
5-
printf("This program transform markdown formated text into the ");
6-
printf("corresponding HTML using the GitHub API v3.\n");
7-
printf("Please, paste your code bellow:(8192 maximum for this test)\n\n");
14+
if (argc <= 1) {
15+
printf("Please, give your markdown file as first argument.\n");
16+
return EXIT_FAILURE;
17+
}
818

9-
char markdown[8192];
10-
if (fgets(markdown, 8192, stdin) == NULL) {
11-
printf("Could not read the standard input\n");
19+
// Open the file given as argument
20+
FILE* file = fopen(argv[1], "r");
21+
if (file == NULL) {
22+
printf("Could not open the file\n");
1223
return EXIT_FAILURE;
1324
}
1425

15-
char* html = get_html_from_markdown(markdown);
26+
// Allocate the memory for the coming file
27+
char* file_content = (char*) malloc(MAX_SIZE * sizeof(char));
28+
if (file_content == NULL) {
29+
printf("Could not allocate enough memory for the purpose.\n");
30+
fclose(file);
31+
return EXIT_FAILURE;
32+
}
1633

34+
// Read the file
35+
char tmp = 0;
36+
int i = -1;
37+
while (i++ < MAX_SIZE) {
38+
tmp = (char) fgetc(file);
39+
if (tmp == EOF) {
40+
// We've reached the end of the file, let's exit now
41+
file_content[i] = '\0';
42+
break;
43+
} else {
44+
file_content[i] = tmp;
45+
}
46+
}
47+
fclose(file);
48+
49+
// Now, we just have to give our file's content to the library :)
50+
char* html = get_html_from_markdown(file_content);
1751
if (html == NULL) {
18-
printf("Something bad happened during the communication with the ");
19-
printf("GitHub API server.\n");
20-
free(markdown);
21-
return EXIT_FAILURE;
52+
printf("Something has happened during the parsing process.\n");
53+
free(file_content);
2254
}
2355

24-
printf("%s\n", html);
56+
printf("%s", html);
2557

26-
free(markdown);
58+
free(file_content);
2759
free(html);
2860

2961
return EXIT_SUCCESS;

sources/ghfmd.c

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,6 @@
22

33

44

5-
/**
6-
* Return the JSON expected by the REST API as a string, ready to be sent over
7-
* the network
8-
*
9-
* @param text The markdown formated text submited
10-
*
11-
* @return The JSON expected by the REST API. It returns NULL if no memory
12-
* could be allocated for the result.
13-
*/
14-
char* get_json(char* text) {
15-
size_t text_len = strlen(text);
16-
17-
// Allocate the memory for the result
18-
// The final result will look like:
19-
// {"text": $text, "mode": "mardown"}
20-
char* json =\
21-
(char*) malloc((text_len + JSON_SQUELETON_SIZE + 1) * sizeof(char));
22-
23-
if (json == NULL) {
24-
return NULL;
25-
}
26-
27-
int num_of_char_written = snprintf(json, (JSON_SQUELETON_SIZE + text_len +\
28-
1), "{\"text\": \"%s\", \"mode\": \"markdown\"}", text);
29-
30-
if (num_of_char_written != (text_len + JSON_SQUELETON_SIZE)) {
31-
// Something bad has happened during the writting process to $json
32-
free(json);
33-
return NULL;
34-
}
35-
36-
return json;
37-
}
385

396

407

@@ -89,28 +56,16 @@ size_t receive_http_data(char* ptr, size_t size, size_t nmemb, void* userdata) {
8956
* problem connected to the libcurl occured
9057
*/
9158
char* get_html_from_markdown(char* markdown) {
92-
// Let's transform our markdown formated text as the expected JSON
93-
char* json = get_json(markdown);
94-
95-
if (json == NULL) {
96-
// A problem has occured during the JSON generation process
97-
return NULL;
98-
}
99-
100-
101-
// Now, let's init curl's data
59+
// Let's init curl's data
10260

10361
// First, the global initialization
10462
if (curl_global_init(CURL_GLOBAL_ALL) != 0) {
105-
free(json);
106-
10763
return NULL;
10864
}
10965

11066
// Easy interface initialization
11167
CURL* curl = curl_easy_init();
11268
if (curl == NULL) {
113-
free(json);
11469
curl_global_cleanup();
11570

11671
return NULL;
@@ -123,26 +78,33 @@ char* get_html_from_markdown(char* markdown) {
12378
curl_easy_setopt(curl, CURLOPT_URL, API_REQUEST_URL);
12479
curl_easy_setopt(curl, CURLOPT_PORT, API_REQUEST_PORT);
12580

81+
// Set the necessary HTTP headers
82+
struct curl_slist* headers = NULL;
83+
headers = curl_slist_append(headers, "Content-Type: text/plain");
84+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
85+
12686
// Bind the callback for the data reception and which data will be called
12787
// with this callback as the 4th argument (see the function descript above)
12888
char* html_resulted = NULL;
12989
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receive_http_data);
13090
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html_resulted);
13191

13292
// Specify the data to send using the POST method
133-
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json);
93+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, markdown);
13494

13595

13696
// And finally send the request
13797
if (curl_easy_perform(curl) != 0) {
13898
// The request could not be sent...
13999
curl_easy_cleanup(curl);
140100
curl_global_cleanup();
141-
free(json);
142101

143102
return NULL;
144103
}
145104

105+
// Free the HTTP headers
106+
curl_slist_free_all(headers);
107+
146108
// Cleanup the stuff done by curl
147109
curl_easy_cleanup(curl);
148110

tests/ghfmd.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,6 @@ int tests_run = 0;
1313

1414

1515

16-
/**
17-
* Compare a "hard-written" result with the one received from the get_json()
18-
* function
19-
*/
20-
char* test_get_json() {
21-
char* text = (char*) malloc(11 * sizeof(char));
22-
strcpy(text, "Hello world");
23-
char* expected_json =\
24-
(char*) malloc((11 + JSON_SQUELETON_SIZE) * sizeof(char));
25-
strcpy(expected_json, "{\"text\": \"Hello world\", \"mode\": \"markdown\"}");
26-
27-
char* json = get_json(text);
28-
29-
mu_assert("The JSON generator didn't returned a correct string",
30-
(strcmp(json, expected_json) == 0));
31-
32-
free(json);
33-
free(text);
34-
free(expected_json);
35-
36-
return 0;
37-
}
38-
3916

4017

4118

@@ -64,7 +41,6 @@ char* test_get_html_from_markdown() {
6441
* Run all defined tests
6542
*/
6643
char* all_tests() {
67-
mu_run_test(test_get_json);
6844
mu_run_test(test_get_html_from_markdown);
6945

7046
return 0;

0 commit comments

Comments
 (0)