Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 629 lines (511 sloc) 11.951 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/*
* theme: use a template to create a webpage (markdown-style)
*
* usage: theme [-d root] [-p pagename] [-t template] [-o html] [source]
*
*/
/*
* Copyright (C) 2007 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include "config.h"
#include "pgm_options.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_BASENAME) && defined(HAVE_LIBGEN_H)
# include <libgen.h>
#endif
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#if HAVE_PWD_H
# include <pwd.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#include "mkdio.h"
#include "cstring.h"
#include "amalloc.h"

char *pgm = "theme";
char *output = 0;
char *pagename = 0;
char *root = 0;
int everywhere = 0; /* expand all <?theme elements everywhere */

#if HAVE_PWD_H
struct passwd *me = 0;
#endif
struct stat *infop = 0;


#define INTAG 0x01
#define INHEAD 0x02
#define INBODY 0x04


#ifndef HAVE_BASENAME
char *
basename(char *path)
{
    char *p;

    if ( p = strrchr(path, '/') )
return 1+p;
    return path;
}
#endif

#ifdef HAVE_FCHDIR
typedef int HERE;
#define NOT_HERE (-1)

#define pushd(d) open(d, O_RDONLY)

int
popd(HERE pwd)
{
    int rc = fchdir(pwd);
    close(pwd);
    return rc;
}

#else

typedef char* HERE;
#define NOT_HERE 0

HERE
pushd(char *d)
{
    HERE cwd;
    int size;
    
    if ( chdir(d) == -1 )
return NOT_HERE;

    for (cwd = malloc(size=40); cwd; cwd = realloc(cwd, size *= 2))
if ( getcwd(cwd, size) )
return cwd;

    return NOT_HERE;
}

int
popd(HERE pwd)
{
    if ( pwd ) {
int rc = chdir(pwd);
free(pwd);

return rc;
    }
    return -1;
}
#endif

typedef STRING(int) Istring;

void
fail(char *why, ...)
{
    va_list ptr;

    va_start(ptr,why);
    fprintf(stderr, "%s: ", pgm);
    vfprintf(stderr, why, ptr);
    fputc('\n', stderr);
    va_end(ptr);
    exit(1);
}


/* open_template() -- start at the current directory and work up,
* looking for the deepest nested template.
* Stop looking when we reach $root or /
*/
FILE *
open_template(char *template)
{
    char *cwd;
    int szcwd;
    HERE here = pushd(".");
    FILE *ret;

    if ( here == NOT_HERE )
fail("cannot access the current directory");

    szcwd = root ? 1 + strlen(root) : 2;

    if ( (cwd = malloc(szcwd)) == 0 )
return 0;

    while ( !(ret = fopen(template, "r")) ) {
if ( getcwd(cwd, szcwd) == 0 ) {
if ( errno == ERANGE )
goto up;
break;
}

if ( root && (strcmp(root, cwd) == 0) )
break; /* ran out of paths to search */
else if ( (strcmp(cwd, "/") == 0) || (*cwd == 0) )
break; /* reached / */

    up: if ( chdir("..") == -1 )
break;
    }
    free(cwd);
    popd(here);
    return ret;
} /* open_template */


static Istring inbuf;
static int psp;

static int
prepare(FILE *input)
{
    int c;

    CREATE(inbuf);
    psp = 0;
    while ( (c = getc(input)) != EOF )
EXPAND(inbuf) = c;
    fclose(input);
    return 1;
}

static int
pull()
{
    return psp < S(inbuf) ? T(inbuf)[psp++] : EOF;
}

static int
peek(int offset)
{
    int pos = (psp + offset)-1;

    if ( pos >= 0 && pos < S(inbuf) )
return T(inbuf)[pos];

    return EOF;
}

static int
shift(int shiftwidth)
{
    psp += shiftwidth;
    return psp;
}

static int*
cursor()
{
    return T(inbuf) + psp;
}


static int
thesame(int *p, char *pat)
{
    int i;

    for ( i=0; pat[i]; i++ ) {
if ( pat[i] == ' ' ) {
if ( !isspace(peek(i+1)) ) {
return 0;
}
}
else if ( tolower(peek(i+1)) != pat[i] ) {
return 0;
}
    }
    return 1;
}


static int
istag(int *p, char *pat)
{
    int c;

    if ( thesame(p, pat) ) {
c = peek(strlen(pat)+1);
return (c == '>' || isspace(c));
    }
    return 0;
}


/* finclude() includes some (unformatted) source
*/
static void
finclude(MMIOT *doc, FILE *out, int flags, int whence)
{
    int c;
    Cstring include;
    FILE *f;

    CREATE(include);

    while ( (c = pull()) != '(' )
;

    while ( (c=pull()) != ')' && c != EOF )
EXPAND(include) = c;

    if ( c != EOF ) {
EXPAND(include) = 0;
S(include)--;

if (( f = fopen(T(include), "r") )) {
while ( (c = getc(f)) != EOF )
putc(c, out);
fclose(f);
}
    }
    DELETE(include);
}


/* fdirname() prints out the directory part of a path
*/
static void
fdirname(MMIOT *doc, FILE *output, int flags, int whence)
{
    char *p;

    if ( pagename && (p = basename(pagename)) )
fwrite(pagename, strlen(pagename)-strlen(p), 1, output);
}


/* fbasename() prints out the file name part of a path
*/
static void
fbasename(MMIOT *doc, FILE *output, int flags, int whence)
{
    char *p;

    if ( pagename ) {
p = basename(pagename);

if ( !p )
p = pagename;

if ( p )
fwrite(p, strlen(p), 1, output);
    }
}


/* ftitle() prints out the document title
*/
static void
ftitle(MMIOT *doc, FILE* output, int flags, int whence)
{
    char *h;
    if ( (h = mkd_doc_title(doc)) == 0 && pagename )
h = pagename;

    if ( h )
mkd_generateline(h, strlen(h), output, flags);
}


/* fdate() prints out the document date
*/
static void
fdate(MMIOT *doc, FILE *output, int flags, int whence)
{
    char *h;

    if ( (h = mkd_doc_date(doc)) || ( infop && (h = ctime(&infop->st_mtime)) ) )
mkd_generateline(h, strlen(h), output, flags|MKD_TAGTEXT);
}


/* fauthor() prints out the document author
*/
static void
fauthor(MMIOT *doc, FILE *output, int flags, int whence)
{
    char *h = mkd_doc_author(doc);

#if HAVE_PWD_H
    if ( (h == 0) && me )
h = me->pw_gecos;
#endif

    if ( h )
mkd_generateline(h, strlen(h), output, flags);
}


/* fconfig() prints out a tabular version of
* tabular versions of the flags.
*/
static void
fconfig(MMIOT *doc, FILE *output, int flags, int whence)
{
    mkd_mmiot_flags(output, doc, (whence & (INHEAD|INTAG)) ? 0 : 1);
}


/* fversion() prints out the document version
*/
static void
fversion(MMIOT *doc, FILE *output, int flags, int whence)
{
    fwrite(markdown_version, strlen(markdown_version), 1, output);
}


/* fbody() prints out the document
*/
static void
fbody(MMIOT *doc, FILE *output, int flags, int whence)
{
    mkd_generatehtml(doc, output);
}

/* ftoc() prints out the table of contents
*/
static void
ftoc(MMIOT *doc, FILE *output, int flags, int whence)
{
    mkd_generatetoc(doc, output);
}

/* fstyle() prints out the document's style section
*/
static void
fstyle(MMIOT *doc, FILE *output, int flags, int whence)
{
    mkd_generatecss(doc, output);
}


/*
* theme expansions we love:
* <?theme date?> -- the document date (file or header date)
* <?theme title?> -- the document title (header title or document name)
* <?theme author?> -- the document author (header author or document owner)
* <?theme version?> -- the version#
* <?theme body?> -- the document body
* <?theme source?> -- the filename part of the document name
* <?theme dir?> -- the directory part of the document name
* <?theme html?> -- the html file name
* <?theme style?> -- document-supplied style blocks
* <?theme include(file)?> -- include a file.
*/
static struct _keyword {
    char *kw;
    int where;
    void (*what)(MMIOT*,FILE*,int,int);
} keyword[] = {
    { "author?>", 0xffff, fauthor },
    { "body?>", INBODY, fbody },
    { "toc?>", INBODY, ftoc },
    { "date?>", 0xffff, fdate },
    { "dir?>", 0xffff, fdirname },
    { "include(", 0xffff, finclude },
    { "source?>", 0xffff, fbasename },
    { "style?>", INHEAD, fstyle },
    { "title?>", 0xffff, ftitle },
    { "version?>", 0xffff, fversion },
    { "config?>", 0xffff, fconfig },
};
#define NR(x) (sizeof x / sizeof x[0])


/* spin() - run through the theme template, looking for <?theme expansions
*/
void
spin(FILE *template, MMIOT *doc, FILE *output)
{
    int c;
    int *p;
    int flags;
    int where = 0x0;
    int i;

    prepare(template);

    while ( (c = pull()) != EOF ) {
if ( c == '<' ) {
if ( peek(1) == '!' && peek(2) == '-' && peek(3) == '-' ) {
fputs("<!--", output);
shift(3);
do {
putc(c=pull(), output);
} while ( ! (c == '-' && peek(1) == '-' && peek(2) == '>') );
}
else if ( (peek(1) == '?') && thesame(cursor(), "?theme ") ) {
shift(strlen("?theme "));

while ( ((c = pull()) != EOF) && isspace(c) )
;

shift(-1);
p = cursor();

if ( where & INTAG )
flags = MKD_TAGTEXT;
else if ( where & INHEAD )
flags = MKD_NOIMAGE|MKD_NOLINKS;
else
flags = 0;

for (i=0; i < NR(keyword); i++)
if ( thesame(p, keyword[i].kw) ) {
if ( everywhere || (keyword[i].where & where) )
(*keyword[i].what)(doc,output,flags,where);
break;
}

while ( (c = pull()) != EOF && (c != '?' && peek(1) != '>') )
;
shift(1);
}
else
putc(c, output);

if ( istag(cursor(), "head") ) {
where |= INHEAD;
where &= ~INBODY;
}
else if ( istag(cursor(), "body") ) {
where &= ~INHEAD;
where |= INBODY;
}
where |= INTAG;
continue;
}
else if ( c == '>' )
where &= ~INTAG;

putc(c, output);
    }
} /* spin */


main(argc, argv)
char **argv;
{
    char *template = "page.theme";
    char *source = "stdin";
    FILE *tmplfile;
    int opt;
    mkd_flag_t flags = MKD_TOC;
    int force = 0;
    MMIOT *doc;
    struct stat sourceinfo;

    opterr=1;
    pgm = basename(argv[0]);

    while ( (opt=getopt(argc, argv, "EfC:c:d:t:p:o:V")) != EOF ) {
switch (opt) {
case 'd': root = optarg;
break;
case 'E': everywhere = 1;
break;
case 'p': pagename = optarg;
break;
case 'f': force = 1;
break;
case 't': template = optarg;
break;
case 'C': if ( strcmp(optarg, "?") == 0 ) {
show_flags(0);
exit(0);
}
else
flags = strtol(optarg, 0, 0);
break;
case 'c': if ( strcmp(optarg, "?") == 0 ) {
show_flags(1);
exit(0);
}
else if ( !set_flag(&flags, optarg) )
fprintf(stderr,"%s: unknown option <%s>", pgm, optarg);
break;
case 'o': output = optarg;
break;
case 'V': printf("theme+discount %s\n", markdown_version);
exit(0);
default: fprintf(stderr, "usage: %s [-V] [-d dir] [-p pagename] [-t template] [-o html] [file]\n", pgm);
exit(1);
}
    }

    tmplfile = open_template(template);

    argc -= optind;
    argv += optind;


    if ( argc > 0 ) {
int added_text=0;

if ( (source = malloc(strlen(argv[0]) + strlen("/index.text") + 1)) == 0 )
fail("out of memory allocating name buffer");

strcpy(source,argv[0]);
if ( (stat(source, &sourceinfo) == 0) && S_ISDIR(sourceinfo.st_mode) )
strcat(source, "/index");

if ( !freopen(source, "r", stdin) ) {
strcat(source, ".text");
added_text = 1;
if ( !freopen(source, "r", stdin) )
fail("can't open either %s or %s", argv[0], source);
}

if ( !output ) {
char *p, *q;
output = alloca(strlen(source) + strlen(".html") + 1);

strcpy(output, source);

if (( p = strchr(output, '/') ))
q = strrchr(p+1, '.');
else
q = strrchr(output, '.');

if ( q )
*q = 0;
else
q = output + strlen(output);

strcat(q, ".html");
}
    }
    if ( output ) {
if ( force )
unlink(output);
if ( !freopen(output, "w", stdout) )
fail("can't write to %s", output);
    }

    if ( !pagename )
pagename = source;

    if ( (doc = mkd_in(stdin, 0)) == 0 )
fail("can't read %s", source ? source : "stdin");

    if ( fstat(fileno(stdin), &sourceinfo) == 0 )
infop = &sourceinfo;

#if HAVE_GETPWUID
    me = getpwuid(infop ? infop->st_uid : getuid());

    if ( (root = strdup(me->pw_dir)) == 0 )
fail("out of memory");
#endif

    if ( !mkd_compile(doc, flags) )
fail("couldn't compile input");

    if ( tmplfile )
spin(tmplfile,doc,stdout);
    else
mkd_generatehtml(doc, stdout);

    mkd_cleanup(doc);
    exit(0);
}
Something went wrong with that request. Please try again.