From 9c45f71e39f0c0ded10d4c837dcaece44dcd379e Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 5 Jan 2020 23:45:15 +0100 Subject: Cut/copy in Storycode format --- libstorycode/storycode.c | 118 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 20 deletions(-) (limited to 'libstorycode/storycode.c') diff --git a/libstorycode/storycode.c b/libstorycode/storycode.c index 139af67..42c4820 100644 --- a/libstorycode/storycode.c +++ b/libstorycode/storycode.c @@ -115,17 +115,15 @@ static void write_run_border(GOutputStream *fh, enum text_run_type t) } -static char *escape_text(const char *in) +static char *escape_text(const char *in, size_t len) { int i, j; - size_t len; size_t nl = 0; size_t np = 0; const char *esc = "*/_"; size_t n_esc = 3; char *out; - len = strlen(in); for ( i=0; itype); + escaped_str = escape_text(run->text+start, len); + write_string(fh, escaped_str); + free(escaped_str); + write_run_border(fh, run->type); +} + + static void write_para(GOutputStream *fh, struct text_run *runs, int n_runs) { int i; for ( i=0; itype ) { case NARRATIVE_ITEM_TEXT: /* FIXME: separate alignment */ if ( write_string(fh, ": ") ) return 1; - write_para(fh, item->runs, item->n_runs); - if ( write_string(fh, "\n") ) return 1; break; case NARRATIVE_ITEM_PRESTITLE: /* FIXME: separate alignment */ if ( write_string(fh, "PRESTITLE: ") ) return 1; - write_para(fh, item->runs, item->n_runs); - if ( write_string(fh, "\n") ) return 1; break; case NARRATIVE_ITEM_BP: /* FIXME: separate alignment */ if ( write_string(fh, "BP: ") ) return 1; - write_para(fh, item->runs, item->n_runs); - if ( write_string(fh, "\n") ) return 1; break; case NARRATIVE_ITEM_SLIDE: /* FIXME: separate slide size */ - if ( write_string(fh, "SLIDE {\n") ) return 1; + if ( write_string(fh, "SLIDE ") ) return 1; + break; + + case NARRATIVE_ITEM_EOP: + if ( write_string(fh, "ENDOFPRESENTATION") ) return 1; + break; + + } + return 0; +} + + +static int write_item(GOutputStream *fh, struct narrative_item *item) +{ + if ( write_starter(fh, item) ) return 1; + switch ( item->type ) { + + case NARRATIVE_ITEM_TEXT: + write_para(fh, item->runs, item->n_runs); + break; + + case NARRATIVE_ITEM_PRESTITLE: + write_para(fh, item->runs, item->n_runs); + break; + + case NARRATIVE_ITEM_BP: + write_para(fh, item->runs, item->n_runs); + break; + + case NARRATIVE_ITEM_SLIDE: + if ( write_string(fh, "{\n") ) return 1; if ( write_slide(fh, item->slide) ) return 1; - if ( write_string(fh, "}\n") ) return 1; + if ( write_string(fh, "}") ) return 1; break; case NARRATIVE_ITEM_EOP: - if ( write_string(fh, "ENDOFPRESENTATION\n") ) return 1; break; } @@ -293,6 +321,55 @@ static int write_item(GOutputStream *fh, struct narrative_item *item) } +int narrative_write_item(Narrative *n, int item, GOutputStream *fh) +{ + return write_item(fh, &n->items[item]); +} + + +int narrative_write_partial_item(Narrative *n, int inum, size_t start, ssize_t end, + GOutputStream *fh) +{ + struct narrative_item *item; + int r1, r2; + size_t r1offs, r2offs; + size_t r1len, r2len; + int r; + + if ( !narrative_item_is_text(n, inum) ) return narrative_write_item(n, inum, fh); + + item = &n->items[inum]; + r1 = narrative_which_run(item, start, &r1offs); + + if ( end >= 0 ) { + r2 = narrative_which_run(item, end, &r2offs); + } else { + r2 = item->n_runs - 1; + } + + r1len = strlen(item->runs[r1].text); + r2len = strlen(item->runs[r2].text); + + if ( end < 0 ) r2offs = r2len; + + if ( write_starter(fh, item) ) return 1; + + for ( r=r1; r<=r2; r++ ) { + if ( (r1==r2) && (r==r1) ) { + write_partial_run(fh, &item->runs[r], r1offs, r2offs - r1offs); + } else if ( r == r1 ) { + write_partial_run(fh, &item->runs[r], r1offs, r1len - r1offs); + } else if ( r == r2 ) { + write_partial_run(fh, &item->runs[r], 0, r2offs); + } else { + write_partial_run(fh, &item->runs[r], 0, strlen(item->runs[r].text)); + } + } + + return 0; +} + + int storycode_write_presentation(Narrative *n, GOutputStream *fh) { int i; @@ -307,6 +384,7 @@ int storycode_write_presentation(Narrative *n, GOutputStream *fh) for ( i=0; in_items; i++ ) { if ( write_item(fh, &n->items[i]) ) return 1; + if ( write_string(fh, "\n") ) return 1; } return 0; -- cgit v1.2.3