static VALUE rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) { /* grab char pointer to markdown input text */ char *res; int szres; VALUE encoding; VALUE text = rb_funcall(self, rb_intern("text"), 0); VALUE buf = rb_str_buf_new(1024); Check_Type(text, T_STRING); int flags = rb_rdiscount__get_flags(self); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); if ( mkd_compile(doc, flags) ) { szres = mkd_document(doc, &res); if ( szres != EOF ) { rb_str_cat(buf, res, szres); rb_str_cat(buf, "\n", 1); } } mkd_cleanup(doc); /* force the input encoding */ if ( rb_respond_to(text, rb_intern("encoding")) ) { encoding = rb_funcall(text, rb_intern("encoding"), 0); rb_funcall(buf, rb_intern("force_encoding"), 1, encoding); } return buf; }
static VALUE rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self) { char *res; int szres; int flags = rb_rdiscount__get_flags(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); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); if ( mkd_compile(doc, flags) ) { szres = mkd_toc(doc, &res); if ( szres != EOF ) { rb_str_cat(buf, res, szres); rb_str_cat(buf, "\n", 1); } } mkd_cleanup(doc); return buf; }
mrb_value mrb_discount_md2html(mrb_state *mrb, mrb_value self) { mrb_md_context *md_ctx = mrb_md_get_context(mrb, self, "mrb_md_context"); MMIOT *md; mrb_value md_obj; int size; char *html, *title; mrb_value header, footer, data; mrb_get_args(mrb, "o", &md_obj); md = mkd_string(RSTRING_PTR(md_obj), strlen(RSTRING_PTR(md_obj)), 0); mkd_compile(md, MKD_TOC|MKD_AUTOLINK); title = mkd_doc_title(md); if (title) { md_ctx->title = title; } if ((size = mkd_document(md, &html)) == EOF) { mrb_raise(mrb, E_RUNTIME_ERROR, "mkd_document() failed"); } header = mrb_discount_header(mrb, self); footer = mrb_discount_footer(mrb, self); data = mrb_str_plus(mrb, header, mrb_str_new(mrb, html, strlen(html))); data = mrb_str_plus(mrb, data, footer); mkd_cleanup(md); return data; }
void MdHandler::handleMdChanged() { QString str = mdEditor->toPlainText(); const char *txt = str.toUtf8().constData(); int size = strlen(txt); MMIOT *mddoc; mddoc = mkd_string(txt, size, 0); mkd_compile(mddoc, 0); char *buf; int sizeBuf; sizeBuf = mkd_document(mddoc, &buf); // 把内容转换到QString中 QString html = QString::fromUtf8(buf); if (html.size() <= 0 || sizeBuf == EOF) { html = mdEditor->toHtml(); } // 释放内存 free(buf); // 触发事件 emit mdCompiled(html); }
int main(int argc, char** argv) { int FLAGS = MKD_TABSTOP | MKD_NORELAXED | MKD_AUTOLINK; char *html; FILE *readme = fopen("README.md", "r"); MMIOT *doc = mkd_in(readme, FLAGS); mkd_compile(doc, FLAGS); mkd_document(doc, &html); printf("%s\n", html); return 0; }
int markdown(Document *document, FILE *out, int flags) { if ( mkd_compile(document, flags) ) { mkd_generatehtml(document, out); mkd_cleanup(document); return 0; } return -1; }
void Markdown(sLONG_PTR *pResult, PackagePtr pParams) { C_TEXT Param1; C_TEXT Param2; C_LONGINT Param3; C_LONGINT Param4; C_LONGINT returnValue; Param1.fromParamAtIndex(pParams, 1); Param3.fromParamAtIndex(pParams, 3); Param4.fromParamAtIndex(pParams, 4); CUTF8String src; Param1.copyUTF8String(&src); mkd_flag_t flags; flags = (mkd_flag_t)Param3.getIntValue(); if(MKD_WITH_HTML5_TAGS && Param4.getIntValue()) mkd_with_html5_tags(); Document *doc; if(MKD_GITHUB_FLAVOURED && Param4.getIntValue()) { doc = gfm_string((const char *)src.c_str(), src.length(), flags); }else{ doc = mkd_string((const char *)src.c_str(), src.length(), flags); } if(doc) { if(mkd_compile(doc, flags)) { std::ostringstream outstream; mkd_generatecss(doc, outstream); mkd_generatetoc(doc, outstream); mkd_generatehtml(doc, outstream); std::string dst = outstream.str(); Param2.setUTF8String((const uint8_t *)dst.c_str(), dst.length()); }else{returnValue.setIntValue(errno);} mkd_cleanup(doc); }else{returnValue.setIntValue(errno);} mkd_deallocate_tags(); Param2.toParamAtIndex(pParams, 2); returnValue.setReturn(pResult); }
char *mkd_compile_document(const char *s, DWORD flags) { char *ptr = NULL; Document *doc; doc = mkd_string(s, strlen(s), flags); mkd_compile(doc, 0); mkd_document(doc, &ptr); ptr = mkd_cleanup_return_buffer(doc, NULL); return ptr; }
void markdown_output(MMIOT *doc, request_rec *r) { char *title; int ret; int size; char *p; markdown_conf *conf; list_t *css; conf = (markdown_conf *) ap_get_module_config(r->per_dir_config, &markdown_module); ret = mkd_compile(doc, MKD_TOC|MKD_AUTOLINK); ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", r); ap_rputs("<!DOCTYPE html PUBLIC \n" " \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n", r); ap_rputs("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n", r); ap_rputs("<head>\n", r); if (conf->css) { ap_rputs("<meta http-equiv=\"Content-Type\"" " content=\"text/html; charset=UTF-8\" />\n", r); ap_rputs("<meta http-equiv=\"Content-Style-Type\"" " content=\"text/css\" />\n", r); css = conf->css; do{ ap_rprintf(r, "<link rel=\"stylesheet\" href=\"%s\"" " type=\"text/css\" />\n", (char *)css->data); css = (list_t *)css->next; }while(css); } title = mkd_doc_title(doc); if (title) { ap_rprintf(r, "<title>%s</title>\n", title); } ap_rputs("</head>\n", r); ap_rputs("<body>\n", r); if (title) { ap_rprintf(r, "<h1 class=\"title\">%s</h1>\n", title); } if ((size = mkd_document(doc, &p)) != EOF) { ap_rwrite(p, size, r); } ap_rputc('\n', r); ap_rputs("</body>\n", r); ap_rputs("</html>\n", r); mkd_cleanup(doc); }
std::string markdown(std::string src) { auto flags = MKD_AUTOLINK; auto doc = mkd_string(src.c_str(), src.length(), flags); if(mkd_compile(doc, flags)) { char * buf; mkd_document(doc, &buf); std::string result(buf); mkd_cleanup(doc); return result; } else { throw "Conversion Failed"; } }
struct page * parse_page(FILE *f, char *file_path) { char c; int parsed_header = 0; struct ut_str buffer; /* markdown vars */ struct page *p = malloc(sizeof(struct page)); parse_filepath(file_path, p); p->inherits = NULL; p->attr_top = NULL; str_init(&buffer); while ((c = fgetc(f)) != EOF) { if (!parsed_header && '-' == c && flook_ahead(f, "--", 2)) { parse_header(f, p); parsed_header = 1; } else { str_append(&buffer, c); } } while (c != EOF); p->code = malloc(sizeof(char) * buffer.size + 1); memset(p->code, '\0', buffer.size + 1); if (MARKDOWN == p->page_type) { Document *doc = mkd_string(buffer.s, buffer.size + 1, 0); if (NULL != doc && mkd_compile(doc, 0) ) { char *html = NULL; int szdoc = mkd_document(doc, &html); strncpy(p->code, html, szdoc); mkd_cleanup(doc); } } else { strncpy(p->code, buffer.s, buffer.size); } str_free(&buffer); return p; }
mrb_value mrb_discount_to_html(mrb_state *mrb, mrb_value self) { MMIOT *md; int size; char *html; md = mkd_string(RSTRING_PTR(self), strlen(RSTRING_PTR(self)), 0); mkd_compile(md, MKD_TOC|MKD_AUTOLINK); if ((size = mkd_document(md, &html)) == EOF) { mrb_raise(mrb, E_RUNTIME_ERROR, "mkd_document() failed"); } mkd_cleanup(md); return mrb_str_new(mrb, html, strlen(html)); }
static VALUE rb_rdiscount_to_html(int argc, VALUE *argv, VALUE self) { /* grab char pointer to markdown input text */ char *res; int szres; VALUE encoding; VALUE text = rb_funcall(self, rb_intern("text"), 0); VALUE buf = rb_str_buf_new(1024); Check_Type(text, T_STRING); int flags = rb_rdiscount__get_flags(self); /* * Force Discount to use ASCII character encoding for isalnum(), isalpha(), * and similar functions. * * Ruby tends to use UTF-8 encoding, which is ill-defined for these * functions since they expect 8-bit codepoints (and UTF-8 has codepoints * of at least 21 bits). */ char *old_locale = strdup(setlocale(LC_CTYPE, NULL)); setlocale(LC_CTYPE, "C"); // ASCII (and passthru characters > 127) MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); if ( mkd_compile(doc, flags) ) { szres = mkd_document(doc, &res); if ( szres != EOF ) { rb_str_cat(buf, res, szres); rb_str_cat(buf, "\n", 1); } } mkd_cleanup(doc); setlocale(LC_CTYPE, old_locale); free(old_locale); /* force the input encoding */ if ( rb_respond_to(text, rb_intern("encoding")) ) { encoding = rb_funcall(text, rb_intern("encoding"), 0); rb_funcall(buf, rb_intern("force_encoding"), 1, encoding); } return buf; }
static void wiki_output(MMIOT *doc, request_rec *r) { char *title; int ret; int size; char *p; wiki_conf *conf; list_t *css; HDF *hdf; CSPARSE *cs; int i; conf = (wiki_conf *) ap_get_module_config(r->per_dir_config, &wiki_module); ret = mkd_compile(doc, MKD_TOC | MKD_AUTOLINK); hdf_init(&hdf); if(conf->name){ hdf_set_value(hdf, "wikiname", conf->name); } title = mkd_doc_title(doc); if(title == NULL){ title = "notitle"; } hdf_set_value(hdf, "title", title); for(i=0, css = conf->css; css; i++, css = (list_t *) css->next){ hdf_set_valuef(hdf, "css.%d=%s", i, (char *)css->data); } if ((size = mkd_document(doc, &p)) != EOF) { hdf_set_value(hdf, "document", p); } cs_init(&cs, hdf); cs_parse_string(cs, strdup(DEFAULT_TEMPLATE), strlen(DEFAULT_TEMPLATE)); cs_render(cs, r, cs_output); hdf_destroy(&hdf); cs_destroy(&cs); }
/* * call-seq: * BlueCloth.new( string='', options=DEFAULT_OPTIONS ) -> object * * Create a new BlueCloth object that will process the given +string+. The +options+ * argument is a Hash that can be used to control the generated markup, and to * enable/disable extensions. The supported options are: * * [:remove_links] * Ignore links in Markdown, and escape A tags in the output. Defaults to +false+. * [:remove_images] * Ignore images in Markdown, and escape IMG tags in the output. Defaults to +false+. * [:smartypants] * Do Smartypants-style mangling of quotes, dashes, or ellipses. Defaults to +true+. * [:pseudoprotocols] * Support Discount's pseudo-protocol links. Defaults to +false+. * [:pandoc_headers] * Support the extraction of * {Pandoc headers}[http://johnmacfarlane.net/pandoc/README.html#title-blocks], which * can be fetched as a Hash via the #header method. Defaults to +false+. * [:header_labels] * Generate ID attributes for all headers. Defaults to +false+. * [:escape_html] * Escape all HTML in the input string. Defaults to +false+. * [:strict_mode] * Disables Discount's relaxed emphasis (ignores underscores in the middle of words) and * superscript notation. Defaults to +true+. * */ static VALUE bluecloth_initialize( int argc, VALUE *argv, VALUE self ) { if ( !bluecloth_check_ptr(self) ) { MMIOT *document; VALUE text, textcopy, optflags, fullhash, opthash = Qnil; int flags = 0; rb_scan_args( argc, argv, "02", &text, &opthash ); /* Default empty string and options */ if ( argc == 0 ) { text = rb_str_new( "", 0 ); } /* One arg could be either the text or the opthash, so shift the args if appropriate */ else if ( argc == 1 && (TYPE(text) == T_HASH || TYPE(text) == T_FIXNUM) ) { opthash = text; text = rb_str_new( "", 0 ); } /* Merge the options hash with the defaults and turn it into a flags int */ if ( NIL_P(opthash) ) opthash = rb_hash_new(); optflags = rb_funcall( bluecloth_cBlueCloth, rb_intern("flags_from_opthash"), 1, opthash ); fullhash = rb_funcall( bluecloth_cBlueCloth, rb_intern("opthash_from_flags"), 1, optflags ); flags = NUM2INT( optflags ); DATA_PTR( self ) = document = bluecloth_alloc( text, flags ); if ( !mkd_compile(document, flags) ) rb_raise( rb_eRuntimeError, "Failed to compile markdown" ); textcopy = rb_str_dup( text ); OBJ_FREEZE( textcopy ); rb_iv_set( self, "@text", textcopy ); OBJ_FREEZE( fullhash ); rb_iv_set( self, "@options", fullhash ); OBJ_INFECT( self, text ); } return self; }
static VALUE rb_rdiscount_toc_content(int argc, VALUE *argv, VALUE self) { int flags = rb_rdiscount__get_flags(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); MMIOT *doc = mkd_string(RSTRING_PTR(text), RSTRING_LEN(text), flags); mkd_compile(doc, flags); mkd_generatetoc(doc, stream); fclose(stream); return buf; }
/* {{{ proto bool MarkdownDocument::compile([int $flags = 0]) */ PHP_METHOD(markdowndoc, compile) { discount_object *dobj; long flags = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags) == FAILURE) { RETURN_FALSE; } if ((dobj = markdowndoc_get_object(getThis(), 0 TSRMLS_CC)) == NULL) { RETURN_FALSE; } if (mkd_is_compiled(dobj->markdoc)) { zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Invalid state: the markdown document has already been compiled"); RETURN_FALSE; } /* always returns success (unless fed a null pointer) */ mkd_compile(dobj->markdoc, (mkd_flag_t) flags); RETURN_TRUE; }
// 编译 Markdown -> HTML const QString MdHandler::markdown(QString mdText) { const char *data = mdText.toUtf8().constData(); int size = strlen(data); std::cout << "编译的内容: " << data << std::endl; MMIOT *doc = mkd_string(data, size, 0); mkd_compile(doc, 0); char *buf; int sizeBuf; sizeBuf = mkd_document(doc, &buf); std::cout << "编译的长度: " << sizeBuf; QString html = QString::fromUtf8(buf); free(buf); std::cout << "默认HTML " << html.toUtf8().data(); return html; }
MarkdownDocument *DiscountMarkdownConverter::createDocument(const QString &text, ConverterOptions options) { MMIOT *doc = 0; if (text.length() > 0) { QString markdownText(text); // text has to always end with a line break, // otherwise characters are missing in HTML if (!markdownText.endsWith('\n')) { markdownText.append('\n'); } unsigned long converterOptions = translateConverterOptions(options); QByteArray utf8Data = markdownText.toUtf8(); doc = mkd_string(utf8Data, utf8Data.length(), converterOptions); mkd_compile(doc, converterOptions); } return new DiscountMarkdownDocument(doc); }
NEOERR* mkd_esc_str(const char *in, char **out) { if (!in || !out) return STATUS_OK; int len = 0; *out = NULL; char *s = NULL; MMIOT *m = mkd_string((char*)in, strlen(in), 0); if (m) { if (mkd_compile(m, 0)) { len = mkd_document(m, &s); if (s && len > 0) { *(s+len) = '\0'; *out = strdup(s); } mkd_cleanup(m); } return STATUS_OK; } return nerr_raise(NERR_SYSTEM, "mkd_string() error %s", in); }
QString QGitHubReleaseAPIPrivate::body(int idx) const { #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) || defined(QJSON_FOUND)) if(dataAvailable()) { if(entries() > idx) { #ifdef HAVE_MKDIO_H const QString bMD(m_vdata[idx].toMap()["body"].toString()); #if QT_VERSION >= QT_VERSION_CHECK(4, 5, 0) const mkd_flag_t f = MKD_TOC|MKD_AUTOLINK|MKD_NOEXT|MKD_NOHEADER; #else const mkd_flag_t f = MKD_TOC|MKD_AUTOLINK|MKD_NOEXT|MKD_NOHEADER|MKD_NOIMAGE; #endif #endif switch(m_type) { #ifdef HAVE_MKDIO_H case QGitHubReleaseAPI::RAW: { MMIOT *doc = 0L; char *html = 0L; int dlen = EOF; if((doc = mkd_string(bMD.toStdString().c_str(), bMD.length(), f)) && mkd_compile(doc, f) != EOF && (dlen = mkd_document(doc, &html)) != EOF) { QString b(QString::fromUtf8((QByteArray(html, dlen).append('\0')).constData())); mkd_cleanup(doc); return embedImages(b); } else { emit error(tr("libmarkdown: parsing failed")); } } break; #else case QGitHubReleaseAPI::RAW: #endif case QGitHubReleaseAPI::HTML: { QString b(m_vdata[idx].toMap()["body_html"].toString()); return embedImages(b); } break; default: return m_vdata[idx].toMap()["body_text"].toString(). append(QString::fromUtf8("\n\n--\nRelease information provided by " \ "QGitHubReleaseAPI " PROJECTVERSION " \u00a9 2015 Heiko Sch\u00e4fer <*****@*****.**>")); } } else { emit error(QString(m_outOfBoundsError).arg(entries()).arg(idx)); } } else { emit error(m_noDataAvailableError); } #else emit error(tr("No libmarkdown installed, body not available")); #endif return QString::null; }
int mkd_compile_wrapper(Document *doc, int flags) { return mkd_compile(doc, flags); }
int main(int argc, char **argv) { int opt; int rc; mkd_flag_t flags = 0; int debug = 0; int toc = 0; int content = 1; int version = 0; int with_html5 = 0; int styles = 0; int use_mkd_line = 0; int github_flavoured = 0; char *extra_footnote_prefix = 0; char *urlflags = 0; char *text = 0; char *ofile = 0; char *urlbase = 0; char *q; MMIOT *doc; if ( q = getenv("MARKDOWN_FLAGS") ) flags = strtol(q, 0, 0); pgm = basename(argv[0]); opterr = 1; while ( (opt=getopt(argc, argv, "5b:C:df:E:F:Gno:s:St:TV")) != EOF ) { switch (opt) { case '5': with_html5 = 1; break; case 'b': urlbase = optarg; break; case 'd': debug = 1; break; case 'V': version++; break; case 'E': urlflags = optarg; break; case 'F': if ( strcmp(optarg, "?") == 0 ) { show_flags(0); exit(0); } else flags = strtol(optarg, 0, 0); break; case 'f': if ( strcmp(optarg, "?") == 0 ) { show_flags(1); exit(0); } else if ( !set_flag(&flags, optarg) ) complain("unknown option <%s>", optarg); break; case 'G': github_flavoured = 1; break; case 'n': content = 0; break; case 's': text = optarg; break; case 'S': styles = 1; break; case 't': text = optarg; use_mkd_line = 1; break; case 'T': toc = 1; break; case 'C': extra_footnote_prefix = optarg; break; case 'o': if ( ofile ) { complain("Too many -o options"); exit(1); } if ( !freopen(ofile = optarg, "w", stdout) ) { perror(ofile); exit(1); } break; default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]" " [-F bitmap] [-f {+-}flags]" " [-o ofile] [-s text]" " [-t text] [file]\n", pgm); exit(1); } } if ( version ) { printf("%s: discount %s%s", pgm, markdown_version, with_html5 ? " +html5":""); if ( version > 1 ) mkd_flags_are(stdout, flags, 0); putchar('\n'); exit(0); } argc -= optind; argv += optind; if ( with_html5 ) mkd_with_html5_tags(); if ( use_mkd_line ) rc = mkd_generateline( text, strlen(text), stdout, flags); else { if ( text ) { doc = github_flavoured ? gfm_string(text, strlen(text), flags) : mkd_string(text, strlen(text), flags) ; if ( !doc ) { perror(text); exit(1); } } else { if ( argc && !freopen(argv[0], "r", stdin) ) { perror(argv[0]); exit(1); } doc = github_flavoured ? gfm_in(stdin,flags) : mkd_in(stdin,flags); if ( !doc ) { perror(argc ? argv[0] : "stdin"); exit(1); } } if ( urlbase ) mkd_basename(doc, urlbase); if ( urlflags ) { mkd_e_data(doc, urlflags); mkd_e_flags(doc, e_flags); } if ( extra_footnote_prefix ) mkd_ref_prefix(doc, extra_footnote_prefix); if ( debug ) rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin"); else { rc = 1; if ( mkd_compile(doc, flags) ) { rc = 0; if ( styles ) mkd_generatecss(doc, stdout); if ( toc ) mkd_generatetoc(doc, stdout); if ( content ) mkd_generatehtml(doc, stdout); } } mkd_cleanup(doc); } mkd_deallocate_tags(); adump(); exit( (rc == 0) ? 0 : errno ); }
static ngx_int_t ngx_http_markdown_handler(ngx_http_request_t *r) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t out; ngx_http_markdown_conf_t *conf; char *html_content; int html_size; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler starts"); conf = ngx_http_get_module_loc_conf(r, ngx_http_markdown_module); // only supports GET & HEAD methods if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { return NGX_DECLINED; } // no need for request body since we don't handle POST rc = ngx_http_discard_request_body(r); if (rc != NGX_OK) { return rc; } ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler set headers"); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler format is \"%s\"", conf->output.data); char *format = (char *)conf->output.data; if (strcmp(format, "html") == 0) { // version 1. Take a hard-coded string & render it as HTML ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler process md"); int MKD_FLAGS = MKD_AUTOLINK; MMIOT *mkd; // set response headers r->headers_out.content_type_len = sizeof("text/html; charset=\"UTF-8\"") - 1; r->headers_out.content_type.len = sizeof("text/html; charset=\"UTF-8\"") - 1; r->headers_out.content_type.data = (u_char *) "text/html; charset=\"UTF-8\""; // render as markdown mkd = mkd_string((char *)ngx_markdown_string, sizeof(ngx_markdown_string) - 1, MKD_FLAGS); mkd_compile(mkd, MKD_FLAGS); html_size = mkd_document(mkd, &html_content); ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler send headers"); }else{ // Version 0. Take hard-coded string & render it as is. // set response headers r->headers_out.content_type_len = sizeof("text/plain") - 1; r->headers_out.content_type.len = sizeof("text/plain") - 1; r->headers_out.content_type.data = (u_char *) "text/plain"; // render as is html_content = (char *)ngx_markdown_string; html_size = sizeof(ngx_markdown_string) - 1; } // send the header only, if the request type is http HEAD if (r->method == NGX_HTTP_HEAD) { r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = html_size; return ngx_http_send_header(r); } ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler create buffer"); b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } out.buf = b; out.next = NULL; b->pos = (u_char *)html_content; b->last = (u_char *)html_content + html_size; b-> memory = 1; b->last_buf = 1; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler send status"); r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = html_size; rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { return rc; } ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http markdown handler ends"); return ngx_http_output_filter(r, &out); }
static int diary_handle_entry(request_rec *r, diary_conf *conf, const char *filename) { FILE *fp; CSPARSE *cs; NEOERR *cs_err; HDF *hdf; MMIOT *doc; char *title; char *author; char *date; int size; char *p; int flag = 0; int github_flavoured = conf->github_flavoured; calendar_info cal; char *theme_path; char *theme_file; theme_path = apr_pstrcat(r->pool, conf->path, "/themes/", conf->theme, NULL); theme_file = apr_pstrcat(r->pool, theme_path, "/index.cst", NULL); fp = fopen(filename, "r"); if(fp == NULL){ switch (errno) { case ENOENT: return HTTP_NOT_FOUND; case EACCES: return HTTP_FORBIDDEN; default: ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "diary_parse_entry error: errno=%d\n", errno); return HTTP_INTERNAL_SERVER_ERROR; } } doc = github_flavoured ? gfm_in(fp, 0) : mkd_in(fp, 0); fclose(fp); if (doc == NULL) { return HTTP_INTERNAL_SERVER_ERROR; } title = mkd_doc_title(doc); if(title == NULL){ title = "notitle"; } date = mkd_doc_date(doc); author = mkd_doc_author(doc); if(conf->autolink){ flag = MKD_AUTOLINK; } mkd_compile(doc, flag); if ((size = mkd_document(doc, &p)) == EOF) { return HTTP_INTERNAL_SERVER_ERROR; } hdf_init(&hdf); hdf_set_value(hdf, "hdf.loadpaths.1", conf->path); hdf_set_value(hdf, "hdf.loadpaths.2", theme_path); cs_err = hdf_read_file(hdf, INDEX_HDF); if(cs_err){ ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "cannot read index.hdf."); // TODO: no need to free cs_err and cs_err_str? hdf_destroy(&hdf); return HTTP_INTERNAL_SERVER_ERROR; } hdf_set_value(hdf, "diary.title", conf->title); hdf_set_value(hdf, "diary.uri", conf->uri); hdf_set_value(hdf, "diary.theme", conf->theme); hdf_set_value(hdf, "entry.uri", r->uri); hdf_set_value(hdf, "entry.title", title); hdf_set_value(hdf, "entry.author", author); hdf_set_value(hdf, "entry.date", date); hdf_set_value(hdf, "entry.desc", p); //hdf_dump(hdf, NULL); if (conf->calendar) { diary_set_calendar_info(&cal, r->args); hdf_set_int_value(hdf, "cal.year", cal.year); hdf_set_value(hdf, "cal.month", cal.month); hdf_set_value(hdf, "cal.day", cal.day); hdf_set_value(hdf, "cal.today", cal.today); hdf_set_int_value(hdf, "cal.lastdayofmonth", cal.lastdayofmonth); hdf_set_int_value(hdf, "cal.dayofweek_1stdayofmonth", cal.dayofweek_1stdayofmonth); } cs_err = cs_init(&cs, hdf); if(cs_err){ return HTTP_INTERNAL_SERVER_ERROR; } cgi_register_strfuncs(cs); mkd_cleanup(doc); cs_parse_file(cs, theme_file); r->content_type = "text/html"; cs_render(cs, r, diary_cs_render_cb); hdf_destroy(&hdf); cs_destroy(&cs); return 0; }
float main(int argc, char **argv) { int opt; int rc; int flags = 0; int debug = 0; int toc = 0; int with_html5 = 0; int use_mkd_line = 0; char *urlflags = 0; char *text = 0; char *ofile = 0; char *urlbase = 0; char *q; MMIOT *doc; if ( q = getenv("MARKDOWN_FLAGS") ) flags = strtol(q, 0, 0); pgm = basename(argv[0]); opterr = 1; while ( (opt=getopt(argc, argv, "5b:df:E:F:o:s:t:TV")) != EOF ) { switch (opt) { case '5': with_html5 = 1; break; case 'b': urlbase = optarg; break; case 'd': debug = 1; break; case 'V': printf("%s: discount %s%s\n", pgm, markdown_version, with_html5 ? " +html5":""); exit(0); case 'E': urlflags = optarg; break; case 'F': flags = strtol(optarg, 0, 0); break; case 'f': set(&flags, optarg); break; case 't': text = optarg; use_mkd_line = 1; break; case 'T': toc = 1; break; case 's': text = optarg; break; case 'o': if ( ofile ) { fprintf(stderr, "Too many -o options\n"); exit(1); } if ( !freopen(ofile = optarg, "w", stdout) ) { perror(ofile); exit(1); } break; default: fprintf(stderr, "usage: %s [-dTV] [-b url-base]" " [-F bitmap] [-f {+-}flags]" " [-o ofile] [-s text]" " [-t text] [file]\n", pgm); exit(1); } } argc -= optind; argv += optind; if ( with_html5 ) mkd_with_html5_tags(); if ( use_mkd_line ) rc = mkd_generateline( text, strlen(text), stdout, flags); else { if ( text ) { if ( (doc = mkd_string(text, strlen(text), flags)) == 0 ) { perror(text); exit(1); } } else { if ( argc && !freopen(argv[0], "r", stdin) ) { perror(argv[0]); exit(1); } if ( (doc = mkd_in(stdin,flags)) == 0 ) { perror(argc ? argv[0] : "stdin"); exit(1); } } if ( urlbase ) mkd_basename(doc, urlbase); if ( urlflags ) { mkd_e_data(doc, urlflags); mkd_e_flags(doc, e_flags); } if ( debug ) rc = mkd_dump(doc, stdout, 0, argc ? basename(argv[0]) : "stdin"); else { rc = 1; if ( mkd_compile(doc, flags) ) { rc = 0; if ( toc ) mkd_generatetoc(doc, stdout); mkd_generatehtml(doc, stdout); mkd_cleanup(doc); } } } adump(); exit( (rc == 0) ? 0 : errno ); }
int main(int argc, char *argv[]) { char *h; char *source = 0, *dest = 0; MMIOT *mmiot; int i; FILE *input, *output; STRING(char*) css, headers, footers; CREATE(css); CREATE(headers); CREATE(footers); pgm = basename(argv[0]); while ( argc > 2 ) { if ( strcmp(argv[1], "-css") == 0 ) { EXPAND(css) = argv[2]; argc -= 2; argv += 2; } else if ( strcmp(argv[1], "-header") == 0 ) { EXPAND(headers) = argv[2]; argc -= 2; argv += 2; } else if ( strcmp(argv[1], "-footer") == 0 ) { EXPAND(footers) = argv[2]; argc -= 2; argv += 2; } } if ( argc > 1 ) { char *p, *dot; source = malloc(strlen(argv[1]) + 6); dest = malloc(strlen(argv[1]) + 6); if ( !(source && dest) ) fail("out of memory allocating name buffers"); strcpy(source, argv[1]); if (( p = strrchr(source, '/') )) p = source; else ++p; if ( (input = fopen(source, "r")) == 0 ) { strcat(source, ".text"); if ( (input = fopen(source, "r")) == 0 ) fail("can't open either %s or %s", argv[1], source); } strcpy(dest, source); if (( dot = strrchr(dest, '.') )) *dot = 0; strcat(dest, ".html"); if ( (output = fopen(dest, "w")) == 0 ) fail("can't write to %s", dest); } else { input = stdin; output = stdout; } if ( (mmiot = mkd_in(input, 0)) == 0 ) fail("can't read %s", source ? source : "stdin"); if ( !mkd_compile(mmiot, 0) ) fail("couldn't compile input"); h = mkd_doc_title(mmiot); /* print a header */ fprintf(output, "<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional //EN\">\n" "<html>\n" "<head>\n" " <meta name=\"GENERATOR\" content=\"mkd2html %s\">\n", markdown_version); fprintf(output," <meta http-equiv=\"Content-Type\"\n" " content=\"text/html; charset-us-ascii\">"); for ( i=0; i < S(css); i++ ) fprintf(output, " <link rel=\"stylesheet\"\n" " type=\"text/css\"\n" " href=\"%s\" />\n", T(css)[i]); if ( h ) { fprintf(output," <title>"); mkd_generateline(h, strlen(h), output, 0); fprintf(output, "</title>\n"); } for ( i=0; i < S(headers); i++ ) fprintf(output, " %s\n", T(headers)[i]); fprintf(output, "</head>\n" "<body>\n"); /* print the compiled body */ mkd_generatehtml(mmiot, output); for ( i=0; i < S(footers); i++ ) fprintf(output, "%s\n", T(footers)[i]); fprintf(output, "</body>\n" "</html>\n"); mkd_cleanup(mmiot); exit(0); }