/* Convert a given markdown file into a HTML file. */ int convert_file(const char *path_in, const char *path_out) { FILE *fin = fopen(path_in, "r"); if (!fin) { printf("Error opening file <%s>\n%s\n", path_in, strerror(errno)); return 1; } FILE *fout = fopen(path_out, "aw"); if (!fout) { printf("Error opening file <%s>\n%s\n", path_out, strerror(errno)); return 1; } Document *doc = mkd_in(fin, 0); if (!doc) { printf("Unknown libmarkdown error calling mkd_in()."); return 1; } int res = 0; if (markdown(doc, fout, 0)) { printf("Unknown libmarkdown error calling markdown()."); res = 1; } fclose(fin); fclose(fout); mkd_cleanup(doc); return res; }
static VALUE rb_redcarpet_to_html(int argc, VALUE *argv, VALUE self) { VALUE text = rb_funcall(self, rb_intern("text"), 0); VALUE result; struct buf input_buf, *output_buf; struct mkd_renderer redcarpet_render; Check_Type(text, T_STRING); memset(&input_buf, 0x0, sizeof(struct buf)); input_buf.data = RSTRING_PTR(text); input_buf.size = RSTRING_LEN(text); output_buf = bufnew(64); rb_redcarpet__setup_render(self, &redcarpet_render); markdown(output_buf, &input_buf, &redcarpet_render); result = rb_str_new(output_buf->data, output_buf->size); bufrelease(output_buf); /* force the input encoding */ if (rb_respond_to(text, rb_intern("encoding"))) { VALUE encoding = rb_funcall(text, rb_intern("encoding"), 0); rb_funcall(result, rb_intern("force_encoding"), 1, encoding); } return result; }
Document Parser::parse(const char* mkd) { document = Document(); if (mkd) { struct buf *ib, *ob; ib = bufnew(INPUT_UNIT); bufputs(ib, mkd); ob = bufnew(OUTPUT_UNIT); mkd_callbacks.opaque = this; //parse and assemble document markdown(ob, ib, &mkd_callbacks); for (std::map<int, Element>::iterator it = elementSoup.begin(); it != elementSoup.end(); ++it) { document.append(it->second); } bufrelease(ib); bufrelease(ob); } return document; }
/** * @brief Parser for HTML files to find encoding information */ static unsigned demoGuessEncoding_html(const char *src, size_t len) { CMarkdown markdown(src, src + len, CMarkdown::Html); //As <html> and <head> are optional, there is nothing to pull... //markdown.Move("html").Pop().Move("head").Pop(); while (markdown.Move("meta")) { CMarkdown::String http_equiv = markdown.GetAttribute("http-equiv"); if (http_equiv.A && lstrcmpiA(http_equiv.A, "content-type") == 0) { CMarkdown::String content = markdown.GetAttribute("content"); if (char *pchKey = content.A) { while (int cchKey = strcspn(pchKey += strspn(pchKey, "; \t\r\n"), ";=")) { char *pchValue = pchKey + cchKey; int cchValue = strcspn(pchValue += strspn(pchValue, "= \t\r\n"), "; \t\r\n"); if (cchKey >= 7 && _memicmp(pchKey, "charset", 7) == 0 && (cchKey == 7 || strchr(" \t\r\n", pchKey[7]))) { pchValue[cchValue] = '\0'; // Is it an encoding name known to charsets module ? unsigned encodingId = FindEncodingIdFromNameOrAlias(pchValue); if (encodingId) { return GetEncodingCodePageFromId(encodingId); } return 0; } pchKey = pchValue + cchValue; } } } } return 0; }
void Parser::updateData(const QString &newData) { QByteArray bytes = newData.toUtf8(); data = tmpfile(); markdown(mkd_string(bytes.data(), bytes.size(), 0), data, 0); rewind(data); }
void MainWindow::textChanged(){ QTime t; t.start(); QString newText = markdown(ui->plainTextEdit->toPlainText()); QString newHTML = wrapInHTMLDoc(newText); renderLabel->setText(QString("Render time: %1 ms").arg(t.elapsed())); QPoint pos = ui->hswv->page()->currentFrame()->scrollPosition(); ui->hswv->setHtml(newHTML); ui->sourceView->setPlainText(newHTML); ui->hswv->page()->currentFrame()->setScrollPosition(pos); }
int main () { std::locale::global (std::locale ("")); std::wcin.imbue (std::locale ("")); std::wcout.imbue (std::locale ("")); std::wstring buf; int ch; while ((ch = std::wcin.get ()) > 0) buf.push_back (ch); markdown (buf, std::wcout); return EXIT_SUCCESS; }
void markdown_to_html( struct Blob *input_markdown, struct Blob *output_title, struct Blob *output_body ){ struct mkd_renderer html_renderer = { /* prolog and epilog */ html_prolog, html_epilog, /* block level elements */ html_blockcode, html_blockquote, html_raw_block, html_header, html_hrule, html_list, html_list_item, html_paragraph, html_table, html_table_cell, html_table_row, /* span level elements */ html_autolink, html_code_span, html_double_emphasis, html_emphasis, html_image, html_line_break, html_link, html_raw_span, html_triple_emphasis, /* low level elements */ 0, /* entities are copied verbatim */ html_normal_text, /* misc. parameters */ 64, /* maximum stack */ "*_", /* emphasis characters */ 0 /* opaque data */ }; html_renderer.opaque = output_title; blob_reset(output_title); blob_reset(output_body); markdown(output_body, input_markdown, &html_renderer); }
static VALUE rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) { /* grab char pointer to markdown input text */ VALUE text = rb_funcall(self, rb_intern("text"), 0); Check_Type(text, T_STRING); /* allocate a ruby string buffer and wrap it in a stream */ VALUE buf = rb_str_buf_new(4096); FILE *stream = rb_str_io_new(buf); int flags = rb_rdiscount__get_flags(self); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); markdown(doc, stream, flags); fclose(stream); return buf; }
/* main • main function, interfacing STDIO with the parser */ int main(int argc, char **argv) { struct buf *ib, *ob; size_t ret; FILE *in = stdin; /* opening the file if given from the command line */ if (argc > 1) { in = fopen(argv[1], "r"); if (!in) { fprintf(stderr,"Unable to open input file \"%s\": %s\n", argv[1], strerror(errno)); return 1; } } /* reading everything */ ib = bufnew(READ_UNIT); bufgrow(ib, READ_UNIT); while ((ret = fread(ib->data + ib->size, 1, ib->asize - ib->size, in)) > 0) { ib->size += ret; bufgrow(ib, ib->size + READ_UNIT); } if (in != stdin) fclose(in); /* performing markdown to LaTeX */ ob = bufnew(OUTPUT_UNIT); markdown(ob, ib, &to_latex); /* writing the result to stdout */ ret = fwrite(ob->data, 1, ob->size, stdout); if (ret < ob->size) fprintf(stderr, "Warning: only %zu output byte written, " "out of %zu\n", ret, ob->size); /* cleanup */ bufrelease(ib); bufrelease(ob); return 0; }