-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtxcmm.c
4128 lines (3728 loc) · 134 KB
/
mtxcmm.c
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:set ts=8 sw=4 et: */
/*
MDVIEW MTX
Copyright (C) 2024 step, https://github.com/step-
Licensed under the GNU General Public License Version 2
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
MDVIEW MTX is a continuation of the mdview3 project.
mdview3 -- http://chiselapp.com/user/jamesbond/repository/mdview3
Copyright (C) 2008 Richard Hughes <[email protected]>
Copyright (C) 2009 Leandro Pereira <[email protected]>
Copyright (C) 2015 James B
Copyright (C) 2016, 2023 step
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_58
#include <glib.h>
#include <libintl.h>
#include <ctype.h>
#include <pango/pango.h>
#include <pango/pango-utils.h>
#include "mtx.h"
#include "mtxcmm.h"
#include "mtxcmmprivate.h"
#include "mtxstylepango.h"
#include "mtxdbg.h"
struct _MtxCmmPrivate
{
gboolean escape;
gboolean escaping; /* cached because frequently used */
gint ctr_repl_eval; /* mtx_cmm_string_release_protected */
MtxCmmParserUnitType seen_unit_types; /* by mtx_cmm_render */
gboolean inside_table; /* in <table> <a> selector */
/* With public getters and setters */
MtxCmmExtensions extensions;
MtxCmmTweaks tweaks;
MtxCmmOutput output;
MtxCmmTags tags; /* viewer sets, renderer gets */
/* Parser queues. */
GQueue *unitq; /* parsed markdown */
gpointer *unitq_head; /* cache top element */
GQueue *junkq; /* parser scraps */
/* Parser work-tables */
GArray *regex_table; /* precompiled regex */
GPtrArray *link_table; /* URL/image and attributes */
GPtrArray *code_table; /* <code>, protect sundries */
};
/**********************************************************************/
/**********************************************************************/
G_DEFINE_TYPE_WITH_CODE (MtxCmm, mtx_cmm, G_TYPE_OBJECT, G_ADD_PRIVATE (MtxCmm))
/**********************************************************************/
/*< private >**********************************************************/
static void mtx_cmm_parser_clear_queues (MtxCmm *);
static void mtx_cmm_parser_clear_regex_table_el (GRegex **e);
static void
mtx_cmm_finalize (GObject *object)
{
MtxCmm *self;
g_return_if_fail (MTX_IS_CMM (object));
self = MTX_CMM (object);
g_return_if_fail (self->priv != NULL);
/* all three with GDestroyNotify function */
g_array_free (self->priv->regex_table, TRUE);
g_ptr_array_free (self->priv->link_table, TRUE);
g_ptr_array_free (self->priv->code_table, TRUE);
mtx_cmm_parser_clear_queues (self);
g_clear_pointer (&self->priv->unitq, g_queue_free); /* NOLINT(bugprone-sizeof-expression) */
g_clear_pointer (&self->priv->junkq, g_queue_free); /* NOLINT(bugprone-sizeof-expression) */
G_OBJECT_CLASS (mtx_cmm_parent_class)->finalize (object);
}
static void
mtx_cmm_class_init (MtxCmmClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = mtx_cmm_finalize;
g_type_ensure (MTX_TYPE_CMM);
}
static void
mtx_cmm_init (MtxCmm *self)
{
self->priv = mtx_cmm_get_instance_private (self);
self->priv->extensions = MTX_CMM_EXTENSION_NONE;
self->priv->tweaks = MTX_CMM_TWEAK_NONE;
#ifndef COMMONMARK_FENCED_CODEBLOCK_LINE_ENDING
self->priv->tweaks &= ~MTX_CMM_TWEAK_CM_BLOCK_END;
#else
self->priv->tweaks |= MTX_CMM_TWEAK_CM_BLOCK_END;
#endif
self->priv->output = MTX_CMM_OUTPUT_UNKNOWN;
self->priv->escape = FALSE;
self->priv->unitq = g_queue_new ();
self->priv->junkq = g_queue_new ();
self->priv->regex_table = g_array_set_size(g_array_sized_new(FALSE, TRUE, sizeof (GRegex *), MTX_CMM_REGEX_LEN), MTX_CMM_REGEX_LEN);
g_array_set_clear_func (self->priv->regex_table, (GDestroyNotify) mtx_cmm_parser_clear_regex_table_el);
self->priv->link_table = g_ptr_array_new_with_free_func (g_free);
self->priv->code_table = g_ptr_array_new_with_free_func (g_free);
}
/*< private >********************************************************/
/*********************************************************************
* PARSER TABLES *
*********************************************************************/
static void
mtx_cmm_parser_clear_regex_table_el (GRegex **e)
{
if (*e)
{
g_regex_unref (*e);
*e = NULL;
}
}
/*********************************************************************
* PARSER QUEUES *
*********************************************************************/
/**
mtx_cmm_parser_unit_cache_head:
Cache and get the current head.
Returns: the current head.
*/
static inline MtxCmmParserUnit *
mtx_cmm_parser_unit_cache_head (MtxCmm *self)
{
return (MtxCmmParserUnit *) (self->priv->unitq_head =
g_queue_peek_head (self->priv->unitq));
}
/**
mtx_cmm_parser_unit_pop_head:
Pop the current parser queue head, and track the new head.
Never free a popped unit. Call mtx_cmm_parser_unit_consume() instead.
Returns: the popped head.
*/
static inline MtxCmmParserUnit *
mtx_cmm_parser_unit_pop_head (MtxCmm *self)
{
MtxCmmParserUnit* head = g_queue_pop_head (self->priv->unitq);
g_queue_push_tail (self->priv->junkq, head);
mtx_cmm_parser_unit_cache_head (self);
return head;
}
/**
mtx_cmm_parser_unit_free_arg:
@argptr: address of a unit argument's character array.
Free the argument.
*/
static void
mtx_cmm_parser_unit_free_arg (gchar **argptr)
{
g_free (*argptr);
}
/**
mtx_cmm_parser_unit_consume:
@unitptr: address of pointer to a %MtxCmmParserUnit.
Never fully remove a unit from its queue. Call this function instead.
*/
static inline void
mtx_cmm_parser_unit_consume (MtxCmmParserUnit **unitptr)
{
(*unitptr)->type = MTX_CMM_PARSER_UNIT_JUNK;
}
/**
mtx_cmm_parser_unit_free:
@self: %MtxCmm instance.
@unit: %MtxCmmParserUnit.
*/
static void
mtx_cmm_parser_unit_free (MtxCmm *self,
MtxCmmParserUnit *unit)
{
if (unit->text)
{
g_string_free (unit->text, TRUE);
unit->text = NULL;
}
if (unit->args)
{
g_array_free (unit->args, TRUE);
unit->args = NULL;
}
if (unit == (MtxCmmParserUnit *) self->priv->unitq_head)
{
self->priv->unitq_head = NULL;
}
g_free (unit);
}
/**
mtx_cmm_parser_unit_clear:
@unit: %MtxCmmParserUnit.
@self: %MtxCmm instance.
Redirected to %mtx_cmm_parser_unit_free.
*/
static inline void
mtx_cmm_parser_unit_clear (MtxCmmParserUnit *unit,
MtxCmm *self)
{
mtx_cmm_parser_unit_free (self, unit);
}
/**
mtx_cmm_parser_clear_queues:
@self: %MtxCmm instance.
Clear parser unit queues leaving GQueue objects allocated.
*/
static void
mtx_cmm_parser_clear_queues (MtxCmm *self)
{
if (self->priv->unitq != NULL)
{
g_queue_foreach (self->priv->unitq, (GFunc) mtx_cmm_parser_unit_clear,
self);
g_queue_clear (self->priv->unitq);
}
self->priv->unitq_head = NULL;
if (self->priv->junkq != NULL)
{
g_queue_foreach (self->priv->junkq, (GFunc) mtx_cmm_parser_unit_clear,
self);
g_queue_clear (self->priv->junkq);
}
}
/*< public >********************************************************/
/**
mtx_cmm_parser_unit_new:
Create a new unit and push it on the private parser queue.
@self: %MtxCmm instance.
@type: %MtxCmmParserUnitType.
@flag_mask: %MtxCmmParserUnitFlag bit mask.
Freeing the new unit with mtx_cmm_parser_unit_free is generally unnecessary as
mtx_cmm_parser_clear_queues() clears parser queues when parsing (re)starts, and
when @self is finalized.
*/
/*static*/ void
mtx_cmm_parser_unit_new (MtxCmm *self,
const MtxCmmParserUnitType type,
const MtxCmmParserUnitFlag flag_mask)
{
MtxCmmParserUnit *head;
head = (MtxCmmParserUnit *) (self->priv->unitq_head =
g_malloc0 (sizeof (MtxCmmParserUnit)));
g_queue_push_head (self->priv->unitq, head);
head->type = type;
head->flag = flag_mask;
if (head->flag & MTX_CMM_PARSER_UNIT_FLAG_ARGS)
{
head->args = g_array_new (TRUE, FALSE, sizeof (gchar *));
g_array_set_clear_func (head->args,
(GDestroyNotify) mtx_cmm_parser_unit_free_arg);
}
}
/**
mtx_cmm_new:
Create a new %MTX_CMM instance.
Returns: the new instance.
*/
MtxCmm *
mtx_cmm_new (void)
{
MtxCmm *self = g_object_new (MTX_TYPE_CMM, NULL);
return MTX_CMM (self);
}
static const gchar * const _tag_info[] = {
[MTX_TAG_DEST_LINK_URI_ID] = "dest=Lu",
[MTX_TAG_DEST_LINK_TXT_LEN] = "dest=Tl",
[MTX_TAG_DEST_IMAGE_PATH_ID] = "dest=Ip",
[MTX_TAG_BLOCKQUOTE_LEVEL] = "blckqtLvl=",
[MTX_TAG_BLOCKQUOTE_OPEN] = "blckqtOpn=",
[MTX_TAG_OL_UL_LEVEL] = "olUlLvl=",
[MTX_TAG_LI_LEVEL] = "liLvl=",
[MTX_TAG_LI_ORDINAL] = "liOrd=",
[MTX_TAG_LI_BULLET_LEN] = "liBLen=",
[MTX_TAG_LI_ID] = "liId=",
};
/**
mtx_cmm_tag_get_info:
@tag: pango "font" tag
@subject: MtxCmmTagInfo
Return: `gint` @subject's value parsed from @tag
*/
gint
mtx_cmm_tag_get_info (MtxCmm *self,
const gchar *tag,
const MtxCmmTagInfo subject)
{
gchar *p = NULL;
gint ret = -1;
g_return_val_if_fail (self != NULL, -1);
g_return_val_if_fail (tag != NULL, -1);
g_return_val_if_fail (subject < MTX_TAG_INFO_LEN, -1);
p = strstr (tag, _tag_info[subject]);
if (p)
{
ret = atoi (p + strlen (_tag_info[subject]));
}
return ret;
}
#if MTX_DEBUG > 1
/* standout, standout end */
#define _SO "\033[7m"
#define _SE "\033[0m"
#define _SObla "\033[7;30m"
#define _SOred "\033[7;31m"
#define _SOgre "\033[7;32m"
#define _SOyel "\033[7;33;46m"
#define _SOblu "\033[7;34m"
#define _SOmag "\033[7;35m"
#define _SOcya "\033[7;36m"
#define _SOwhi "\033[7;37m"
static void
_print_priv_table (const GPtrArray *priv_table,
const gchar *fmt,
...)
{
va_list args;
va_start (args, fmt);
gchar *msg = g_strdup_vprintf (fmt, args);
va_end (args);
g_printerr ("%s", msg);
g_free (msg);
if (priv_table->len == 0)
{
g_printerr (_SO "EMPTY TABLE" _SE "\n");
}
else
{
for (guint i = 0; i < priv_table->len; i++)
{
gchar *p = g_ptr_array_index (priv_table, i);
g_printerr ("% 3d. \"%s\"\n", i, p);
}
}
}
#endif /* MTX_DEBUG > 1 */
/**
mtx_cmm_make_code_ref:
@id: id of an inline code segment stashed with `mtx_cmm_stash_code`.
Return:
dynamically-allocated string code_ref, which encodes @id
*/
static inline gchar*
mtx_cmm_make_code_ref (const guint id)
{
return g_strdup_printf ("%s%dC;%s", sUNIPUA_CODE, id, sUNIPUA_CODE);
}
/**
mtx_cmm_get_code_id:
@ref code_ref text
Return: -1 if @text isn't a valid code_ref otherwise return the code id
*/
static inline gint
mtx_cmm_get_code_id (
const gchar *ref)
{
gint id = -1;
if (strncmp (ref, sUNIPUA_CODE, sizeof (sUNIPUA_CODE) - 1) == 0)
{
id = atoi (ref + sizeof (sUNIPUA_CODE) - 1);
}
return id;
}
/**
mtx_cmm_stash_code:
@code: markdown code span or `code_ref`
Return:
stashed `code_id` - use `mtx_cmm_get_code` to retrieve the code text.
Line endings are converted to space according to the CM spec.
Identical @codes are stashed with the same id.
*/
static inline guint
mtx_cmm_stash_code (MtxCmm *self,
const gchar *code)
{
gint id = mtx_cmm_get_code_id (code);
if (id < 0)
{
gchar *p;
for (guint i = 0; i < self->priv->code_table->len; i++)
{
p = g_ptr_array_index (self->priv->code_table, i);
if (strcmp (code, p) == 0)
{
id = i;
break;
}
}
if (id < 0)
{
p = g_strdup (code);
g_ptr_array_add (self->priv->code_table, p);
id = self->priv->code_table->len - 1;
}
}
return id;
}
/**
mtx_cmm_get_code:
@id: id of a code span previously stashed with `mtx_cmm_stash_code`.
Return: code text containing unescaped grave accents. The instance owns the
returned memory.
*/
static gchar *
mtx_cmm_get_code (MtxCmm *self,
const gint id)
{
g_return_val_if_fail(id < (gint) self->priv->code_table->len, NULL);
return g_ptr_array_index (self->priv->code_table, id);
}
/**
mtx_cmm_regex_code_ref:
Return: a GRegex that matches the internal code_refs created by
mtx_cmm_make_code_ref.
*/
static GRegex *
mtx_cmm_regex_code_ref (MtxCmm *self)
{
static GRegex **regex = NULL;
if (regex == NULL)
{
regex =
&g_array_index (self->priv->regex_table, GRegex *,
MTX_CMM_REGEX_CODE_REF);
}
if (*regex == NULL)
{
*regex = g_regex_new (sUNIPUA_CODE "(\\d+)C;" sUNIPUA_CODE, 0, 0, NULL);
}
return *regex;
}
/**
mtx_cmm_get_link_dest_id:
@ref: a `link_dest_ref`
Return: the link DEST id otherwise -1 if @ref is invalid.
*/
static inline gint
mtx_cmm_get_link_dest_id (const gchar *ref)
{
gint id = -1;
if (strncmp (ref, sUNIPUA_LINK, sizeof (sUNIPUA_LINK) - 1) == 0)
{
id = atoi (ref + sizeof (sUNIPUA_LINK) - 1);
}
return id;
}
/**
mtx_cmm_stash_link_dest:
@dest: markdown link destination or `link_dest_ref`
Return: stashed `link_dest_id`. Use %mtx_cmm_get_link_dest to retrieve the link
destination. Identical destinations are stashed with the same id.
This function also applies to markdown image path.
*/
static gint
mtx_cmm_stash_link_dest (MtxCmm *self,
const gchar *dest)
{
gint id = mtx_cmm_get_link_dest_id (dest);
if (id < 0)
{
gchar *p;
for (guint i = 0; i < self->priv->link_table->len; i++)
{
p = g_ptr_array_index (self->priv->link_table, i);
if (strcmp (dest, p) == 0)
{
id = i;
break;
}
}
if (id < 0)
{
p = g_strdup (dest);
g_ptr_array_add (self->priv->link_table, p);
id = self->priv->link_table->len - 1;
}
}
return id;
}
/**
mtx_cmm_get_link_dest:
@id: link destination id in @self's link_table.
Return: pointer to link URI or image path strings owned by the instance.
*/
const gchar *
mtx_cmm_get_link_dest (MtxCmm *self,
const gint id)
{
g_return_val_if_fail (id < (gint) self->priv->link_table->len, NULL);
return (const gchar *) g_ptr_array_index(self->priv->link_table, id);
}
/**
mtx_cmm_mtx_reset:
*/
static void
mtx_cmm_mtx_reset (MtxCmm *self)
{
gsize i, sz;
gchar **a;
a = (gchar **) g_ptr_array_steal (self->priv->link_table, &sz);
for (i = 0; i < sz; i++)
{
g_free (a[i]);
}
g_free (a);
a = (gchar **) g_ptr_array_steal (self->priv->code_table, &sz);
for (i = 0; i < sz; i++)
{
g_free (a[i]);
}
g_free (a);
mtx_cmm_parser_clear_queues (self);
g_queue_free (self->priv->unitq);
g_queue_free (self->priv->junkq);
self->priv->unitq = g_queue_new ();
self->priv->junkq = g_queue_new ();
}
/**
mtx_cmm_protect:
Stash text in @self->priv->code_table and return the corresponding `code_ref`.
@self: the %MtxCmm instance.
@text: the text to stash and protect.
Return: a `code_ref` string that can be used to replace @text in its original
location. The caller owns the memory. Return NULL on error.
This function is used to encode text in opaque strings, hence preventing the
text from entangling with other text in subsequent parsing and rendering stages.
Use %mtx_cmm_string_release_protected to see the content of a a string containing
protected segments.
*/
static inline gchar *
mtx_cmm_protect (MtxCmm *self,
const gchar *text)
{
gint id = mtx_cmm_stash_code (self, text);
return id < 0 ? NULL : mtx_cmm_make_code_ref (id);
}
/**
mtx_replace_code_ref_cb:
Callback to replace a code_ref match with the referenced text.
*/
static gboolean
mtx_release_code_ref_cb (const GMatchInfo *match_info,
GString *buf,
gpointer data)
{
MtxCmm *self = data;
gchar *match = g_match_info_fetch (match_info, 1);
gint id = atoi (match);
gchar *code = mtx_cmm_get_code (self, id);
g_string_append (buf, code);
g_free (match);
self->priv->ctr_repl_eval++;
return FALSE;
}
/**
mtx_cmm_string_release_protected:
Replace all code_refs in @str with the text they reference.
@str: GString
Returns: the number of matches or -1 on error.
*/
static gint
mtx_cmm_string_release_protected (MtxCmm *self,
GString *str)
{
GRegex *regex = mtx_cmm_regex_code_ref (self);
GError *err = NULL;
self->priv->ctr_repl_eval = 0; /* NON-RE-ENTRANT */
g_autofree gchar *temp =
g_regex_replace_eval (regex, str->str, -1, 0, 0,
mtx_release_code_ref_cb, self, &err);
if (err != NULL)
{
g_printerr ("%s: %s\n", __FUNCTION__, err->message);
g_error_free (err);
return -1;
}
g_string_assign (str, temp);
return self->priv->ctr_repl_eval;
}
/**
mtx_strstrip_pango_markup:
Returns: TRUE and sets *@retptr to a newly-allocated buffer of plain text,
otherwise it returns FALSE for error.
*/
inline static gboolean
mtx_strstrip_pango_markup (const gchar *string,
gchar **retptr)
{
PangoAttrList *attrs;
gboolean ret =
pango_parse_markup (string, -1, 0, &attrs, retptr, NULL, NULL);
{
pango_attr_list_unref (attrs); /* don't care */
}
return ret;
}
/**
mtx_strstrip_pango_spans_fast:
Naïve Pango <span> and <tt> tag stripper.
@string: a string to be modified in place.
This function does not allocate memory it changes @string directly and only
erases <span>, <tt> and their closing tags without semantic checking. Look at
`mtx_strstrip_pango_markup` for a complete and robust stripper.
*/
static void
mtx_strstrip_pango_spans_fast (gchar *string)
{
gchar *p, *q, **t;
const gchar *tags[] = {"<span ", "</span>", "<span>", "<tt>", "</tt>", 0};
for (t = (gchar **) tags; *t != NULL; t++)
{
while ((p = strstr (string, *t)) != NULL)
{
if ((q = strchr (p, '>')) != NULL)
{
memmove (p, q + 1, strchr (q, '\0') - q);
}
}
}
}
/**
mtx_cmm_string_release_protected_unmarked:
Recursively release all code_refs in @str then strip off Pango <span> markup
leaving only clear UTF-8 text, possibly containing UNIPUA codepoints, as the
result.
@str: GString
Returns: the number of matches or -1 on error. In either case @str->str
may have changed.
*/
static gint
mtx_cmm_string_release_protected_unmarked (MtxCmm *self,
GString *str)
{
gint ret, ctr = 0;
do {
ret = mtx_cmm_string_release_protected (self, str);
if (ret > 0)
{
ctr += ret;
}
} while (ret > 0);
if (ctr > 0)
{
#if 0
gchar *clear_text;
(void) mtx_strstrip_pango_markup (str->str, &clear_text);
g_string_assign (str, clear_text);
g_free (clear_text);
#else
/* not very robust but much faster; let's see it in practice... */
mtx_strstrip_pango_spans_fast (str->str);
str->len = strlen (str->str);
#endif
}
return ret < 0 ? -1 : ctr;
}
/**
mtx_cmm_linkbuilder_pango:
@text: can be NULL.
@dest: can be NULL, forwarded as font span.
@title: can be NULL.
@link_dest_id: can be -1.
@dest: the uri-encoded link followed by '\n' followed by the verbatim link.
*/
static gchar *
mtx_cmm_linkbuilder_pango (MtxCmm *self,
const gchar *text,
const gchar *dest,
const gchar *title,
const gint link_dest_id)
{
/*
Here and in mtx_cmm_imagebuilder_pango(), We need to pass extra data
directly to the application that will render the link in the MTX TextView.
Our data will piggyback the Pango markup "font" attribute. The TextView will
receive the font string as a GtkTextTag, decode it to get the font attribute
(string), decode the attribute and call mtx_cmm_get_link_dest() et al. to
obtain the link DEST.
The font attribute string looks like this:
font="@dest=<type><id>[<type><value>...]", with
<type> "Lu"(URI), "Ip"(image path); <id> int(link table index aka id)
The optional <type><value> continuation is only used for "Lu" to add
the length of the link text. For example, markdown [texty](link) yields
font="@dest=Lu1dest=Tl5" assuming it's the first link in the document.
The "@..." syntax is called font VARIATIONS, Pango supports it since version
??. VARIATIONS is a comma-separated list of font variation specifications
of the form @axis=value (the = sign is optional). It's the last part of the
font description{1}. Is it safe to piggyback it? I tested <span>s using
`pango-view --markup -t`, e.g. font="Serif Bold Italic 44 @x=1,y=3,z=4",
and played with "Serif", "Bold", "Italic", "44". It has always worked.
CAVEAT: the added font property shadows other font properties set by outer
spans. I suspect this is inherent in the GtkTextBuffer implementation: no
font style inheritance of the kind we can enjoy with CSS.
{1} https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html
{2} https://gitlab.gnome.org/GNOME/pango/-/blob/main/pango/fonts.c#L1252
is the "[src]" link in {1}
*/
gchar *ret = NULL, *esc_title = NULL;
gchar *style = MTX_STYLE_PANGO_URL;
GString *markup = NULL;
guint llen;
gchar *plain_text = NULL, *merge_img = NULL;
/*
The viewer app will retrieve the link URI from the link_table,
and get: dest format: <uri-encoded>\n<verbatim>.
Incoming 'text' is markdown link text, which can include pango markup now
but will turn into plain text in the GtkTextBuffer. Therefore, on purpose,
'llen' is the length of the link text after stripping markup, as it will
appear to the viewer app.
Said markup could include a markdown image (see examples/text-links-patch.md
as to why). If so, we need to merge the font description of the image into
the font description of the link.
*/
/*
TODO: Support more than one image in link text. For now it's just 0 or 1.
*/
if (text)
{
gint id = -1, repl_ctr = -1;
gchar *p;
mtx_dbg_errout (-1, "(%s)", text);
/* Text could be encoded; we must decode to tell. */
markup = g_string_new (text);
repl_ctr = mtx_cmm_string_release_protected (self, markup);
/* repl_ctr > 0 => text was encoded */
mtx_dbg_errseq (-1, " ==> markup(%s)", markup->str);
(void) mtx_strstrip_pango_markup (markup->str, &plain_text);
mtx_dbg_errseq (-1, " ==> plain_text(%s)", plain_text);
/*
If text encoded (repl_ctr > 0) a markdown image (id >= 0),
initialize a merge argument for the link font description.
*/
if (repl_ctr > 0)
{
id = mtx_cmm_tag_get_info (self, markup->str,
MTX_TAG_DEST_IMAGE_PATH_ID);
if (id >= 0)
{
merge_img =
g_strdup_printf ("@%s%d",
_tag_info[MTX_TAG_DEST_IMAGE_PATH_ID], id);
mtx_dbg_errseq (-1, " ==> merge_img(%s)", merge_img);
p = g_strdup_printf ("font=\"%s\"", merge_img);
g_string_replace (markup, p, "", 1);
g_free (p);
mtx_dbg_errseq (-1, " ==> markup(%s)", markup->str);
}
}
g_string_assign (markup, p = mtx_cmm_protect (self, markup->str));
g_free (p);
mtx_dbg_errseq (-1, " ==> protected_markup(%s)", markup->str);
}
else
{
markup = g_string_new ("⯅⯅");
}
llen =
plain_text != NULL ? (gsize) g_utf8_strlen (plain_text, -1) : markup->len;
g_free (plain_text);
mtx_dbg_errseq (-1, " [ markup %s ]", markup->str);
mtx_dbg_errseq (-1, " [ llen %d ]\n", llen);
if (title)
{
esc_title = g_markup_printf_escaped (" (%s)", title);
}
if (dest && link_dest_id >= 0)
{
ret =
g_strdup_printf ("<span font=\"%s@%s%d%s%d%s\" %s>" "%s</span>%s",
self->priv->inside_table ? "monospace " : "",
_tag_info[MTX_TAG_DEST_LINK_URI_ID], link_dest_id,
_tag_info[MTX_TAG_DEST_LINK_TXT_LEN], llen,
merge_img != NULL ? merge_img : "",
style, markup->str, esc_title ? esc_title : "");
}
else /* link w/o dest, e.g. [text]() is valid CommonMark. */
{
ret =
g_strdup_printf ("<span %s%s>%s</span>%s",
self->priv->inside_table ? "font=\"monospace\"" : "",
style, markup->str, esc_title ? esc_title : "");
}
g_string_free (markup, TRUE);
g_free (merge_img);
g_free (esc_title);
return ret;
}
/**
mtx_cmm_linkbuilder_html:
@text: can be NULL.
@dest: can be NULL.
@title: can be NULL.
@link_dest_id: ignored.
@dest: the uri-encoded link followed by '\n' followed by the verbatim link.
*/
static gchar *
mtx_cmm_linkbuilder_html (MtxCmm *self __attribute__((unused)),
const gchar *text,
const gchar *dest,
const gchar *title,
const gint link_dest_id __attribute__((unused)))
{
gchar *ret, *esc_title = NULL;
/* dest format: <uri-encoded>\n<verbatim> */
*strchr (dest, '\n') = '\0';
if (title)
{
esc_title = g_markup_escape_text (title, -1);
}
if (esc_title)
{
ret = g_strdup_printf ("<a href=\"%s\" title=\"%s\">%s</a>", dest ?
dest : "", esc_title, text ? text : "");
}
else
{
ret = g_strdup_printf ("<a href=\"%s\">%s</a>", dest ? dest : "", text
? text : "");
}
g_free (esc_title);
return ret;
}
/**
mtx_cmm_linkbuilder_text:
@text: can be NULL.
@dest: can be NULL.
@title: can be NULL.
@link_dest_id: ignored.
@dest: the uri-encoded link followed by '\n' followed by the verbatim link.
*/
static gchar *
mtx_cmm_linkbuilder_text (MtxCmm *self __attribute__((unused)),
const gchar *text,
const gchar *dest,
const gchar *title,
const gint link_dest_id __attribute__((unused)))
{
gchar *p;
/* dest format: <uri-encoded>\n<verbatim> */
dest = strchr (dest, '\n') + 1;
if (title)
{
p = g_strdup_printf ("%s%s%s%s%s%s(%s)", text ? text : "", text &&
dest ? " " : "",
dest ? "<" : "", dest ? dest : "", dest ? ">" :
"", text || dest ? " " : "", title);
}
else
{
p = g_strdup_printf ("%s%s%s%s%s", text ? text : "", text && dest ?
" " : "",
dest ? "<" : "", dest ? dest : "", dest ? ">" :
"");
}
return p;
}
/**
mtx_cmm_linkbuilder_ansi:
@text: can be NULL.
@dest: can be NULL.
@title: can be NULL.
@link_dest_id: ignored.