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/narrative.c | 44 +++++++++++++++++- libstorycode/storycode.c | 118 +++++++++++++++++++++++++++++++++++++++-------- libstorycode/storycode.h | 4 +- 3 files changed, 144 insertions(+), 22 deletions(-) diff --git a/libstorycode/narrative.c b/libstorycode/narrative.c index 5dc3f7f..c7e1e31 100644 --- a/libstorycode/narrative.c +++ b/libstorycode/narrative.c @@ -592,7 +592,49 @@ Slide *narrative_get_slide_by_number(Narrative *n, int pos) /* Return the text between item p1/offset o1 and p2/o2 */ char *narrative_range_as_storycode(Narrative *n, int p1, size_t o1, int p2, size_t o2) { - return strdup(": Text"); + GOutputStream *fh; + char *text; + int i; + + fh = g_memory_output_stream_new_resizable(); + if ( fh == NULL ) return NULL; + + for ( i=p1; i<=p2; i++ ) { + + if ( ((i>p1) && (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; diff --git a/libstorycode/storycode.h b/libstorycode/storycode.h index cd98667..6ac2e1f 100644 --- a/libstorycode/storycode.h +++ b/libstorycode/storycode.h @@ -50,6 +50,8 @@ extern char unitc(enum length_unit unit); extern Narrative *storycode_parse_presentation(const char *sc); extern int storycode_write_presentation(Narrative *n, GOutputStream *fh); - +extern int narrative_write_item(Narrative *n, int item, GOutputStream *fh); +extern int narrative_write_partial_item(Narrative *n, int inum, size_t start, ssize_t end, + GOutputStream *fh); #endif /* STORYCODE_H */ -- cgit v1.2.3