1

I'm writing a markdown editor (C++/Qt) and i'm using discount library for that purpose.

Documentation: http://www.pell.portland.or.us/~orc/Code/discount/

i wrote that code to convert HTML to markdown.

#include <mkdio.h>
#include <stdio.h>
#include <string.h>
int main()
{
    FILE *out;
    out = fopen("/home/abdeljalil/test.html","w");
    const char* mkdown= "__hello__";
    MMIOT *doc;
    int flags = MKD_TOC|MKD_SAFELINK|MKD_EXTRA_FOOTNOTE;
    doc = mkd_string(mkdown,strlen(mkdown),flags);
    mkd_compile(doc,flags);
    mkd_generatehtml(doc,out);
    mkd_cleanup(doc);
}

is using output file an efficient method? (i will update the GUI every time markdown is changed in the editor) can i write HTML directly to a string instead of file? (can't find such function) is there any other notes to optimize the code?

Abdeljalil
  • 23
  • 3

1 Answers1

0

Markdown is sort of notorious for being a bit hacked together, nonstandard, and contradictory. Anyone (including myself) who has tried to write a Markdown-to-visual system can tell you just how puzzling/maddening it is. I don't know about "discount" but see CommonMark.org for some current state of thinking from formerly-of-StackOverflow-Jeff and others.

Doing a full reformat of the document on each edit (on entry to idle so as not to block user input) to produce a markdown preview is probably okay for modestly sized documents. Haven't looked at the StackOverflow JavaScript but it is probably doing precisely that.

Your library documentation says:

There are 17 public functions in the markdown library, broken into three categories:

Those functions are file based. As far as I know, you aren't going to find any platform-independent convenience layer allowing you to pass a std::stringstream or otherwise as a C stream FILE *:

cstdio streams vs iostream streams?

You could look into fmemopen to avoid the file creation and write to a buffer, though:

http://www.gnu.org/software/libc/manual/html_node/String-Streams.html

So perhaps investigate that.

Finding the size of a file created by fmemopen

More generally, I might suggest that starting from scratch to wrap a random C-based FILE stream Markdown library up in a Qt editor is a bit of a fool's errand. Either embrace an existing project like CuteMarkEd or embed a JavaScript engine to run the common markdown code, or... something.

Community
  • 1
  • 1
  • @Abdeljalil Also, consider things like [StackEdit](https://stackedit.io/) and embedding WebKit. I wasn't entirely joking about not messing around with "discount" libraries. There is likely no future in that; you might as well embrace something more than one person uses that isn't hardcoded to a 17 function interface in raw C that is not likely to be maintained in the face of a harder-than-it-looks spec. – HostileFork says dont trust SE Oct 25 '14 at 15:14