Example #1
0
/*
 *  call-seq:
 *     bluecloth.header   -> hash
 *
 *  Return the hash of
 *  {Pandoc-style headers}[http://johnmacfarlane.net/pandoc/README.html#title-blocks]
 *  from the parsed document. If there were no headers, or the BlueCloth object was not
 *  constructed with the :pandoc_headers option enabled, an empty Hash is returned.
 *
 *     markdown = "%title My Essay\n%author Me\n%date Today\n\nSome stuff..."
 *     bc = BlueCloth.new( markdown, :pandoc_headers => true )
 *     # =>
 *     bc.header
 *     # =>
 */
static VALUE
bluecloth_header( VALUE self ) {
    MMIOT *document = bluecloth_get_ptr( self );
    char *field;
    VALUE fieldstring, headers = rb_hash_new();

    bluecloth_debug( "Fetching pandoc headers for document %p", document );

    if ( (field = mkd_doc_title(document)) ) {
        fieldstring = rb_str_new2( field );
        OBJ_INFECT( fieldstring, self );
        rb_hash_aset( headers, ID2SYM(rb_intern("title")), fieldstring );
    }
    if ( (field = mkd_doc_author(document)) ) {
        fieldstring = rb_str_new2( field );
        OBJ_INFECT( fieldstring, self );
        rb_hash_aset( headers, ID2SYM(rb_intern("author")), fieldstring );
    }
    if ( (field = mkd_doc_date(document)) ) {
        fieldstring = rb_str_new2( field );
        OBJ_INFECT( fieldstring, self );
        rb_hash_aset( headers, ID2SYM(rb_intern("date")), fieldstring );
    }

    return headers;
}
Example #2
0
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 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);
}
Example #4
0
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);
}
Example #5
0
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;
}
Example #6
0
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);
}