41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
-
+
+
|
MKDA_EXPLICIT_EMAIL, /* e-mail link with explicit mailto: */
MKDA_IMPLICIT_EMAIL /* e-mail link without mailto: */
};
/* mkd_tagspan -- type of tagged <span> */
enum mkd_tagspan {
MKDT_ATREF, /* @name references, as in /chat attention targeting */
MKDT_HASHTAG, /* #hash tags, message IDs, etc. */
MKDT_HASHTAG, /* #hashtags */
MKDT_NUMTAG /* #123[.456] /chat or /forum message IDs. */
};
/* mkd_renderer -- functions for rendering parsed data */
struct mkd_renderer {
/* document level callbacks */
void (*prolog)(struct Blob *ob, void *opaque);
void (*epilog)(struct Blob *ob, void *opaque);
|
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
|
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
|
-
+
+
+
+
+
+
+
+
+
-
+
+
-
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
-
-
+
-
+
+
+
+
+
-
+
-
+
+
+
|
struct render *rndr,
char *data,
size_t offset,
size_t size
){
size_t end;
struct Blob work = BLOB_INITIALIZER;
int nUscore = 0; /* Consecutive underscore counter */;
int nUscore = 0; /* Consecutive underscore counter */
int numberMode = 0 /* 0 for normal, 1 for #NNN numeric,
and 2 for #NNN.NNN. */;
if(offset>0 && !fossil_isspace(data[-1])){
/* Only ever match if the *previous* character is whitespace or
we're at the start of the input. Note that we rely on fossil
processing emphasis markup before reaching this function, so
*#Hash* will Do The Right Thing. Not that this means that
"#Hash." will match while ".#Hash" won't. That's okay. */
return 0;
}
assert( '#' == data[0] );
if(size < 2) return 0;
end = 2;
if(fossil_isdigit(data[1])){
numberMode = 1;
}else if(!fossil_isalpha(data[1])){
switch(data[1] & 0xF0){
/* Reminder: UTF8 char lengths can be determined by
masking against 0xF0: 0xf0==4, 0xe0==3, 0xc0==2,
else 1. */
case 0xF0: end+=3; break;
case 0xE0: end+=2; break;
case 0xC0: end+=1; break;
return 0;
default: return 0;
}
}
#if 0
fprintf(stderr,"HASHREF offset=%d size=%d: %.*s\n",
(int)offset, (int)size, (int)size, data);
#endif
#define HASHTAG_LEGAL_END \
case ' ': case '\t': case '\r': case '\n': \
case ':': case ';': case '!': case '?': case ','
/* ^^^^ '.' is handled separately */
for(end = 2; end < size; ++end){
for(; end < size; ++end){
char ch = data[end];
/* Potential TODO: if (ch & 0xF0), treat it as valid, skip that
multi-byte character's length characters, and continue
looping. Reminder: UTF8 char lengths can be determined by
masking against 0xF0: 0xf0==4, 0xe0==3, 0xc0==2, else 1. */
switch(ch & 0xF0){
case 0xF0: end+=3; continue;
case 0xE0: end+=2; continue;
case 0xC0: end+=1; continue;
case 0x80: goto hashref_bailout /*invalid UTF8*/;
default: break;
}
#if 0
fprintf(stderr,"hashtag? checking... length=%d: %.*s\n",
(int)end, (int)end, data);
#endif
switch(ch){
case '_':
/* Multiple adjacent underscores not permitted. */
if(numberMode>0 || ++nUscore>1) goto hashref_bailout;
if(++nUscore>1) goto hashref_bailout;
numberMode = 0;
break;
case '.':
if(1==numberMode) ++numberMode;
ch = 0;
break;
HASHTAG_LEGAL_END:
if(numberMode==0 && end<3){
goto hashref_bailout/*require 2+ characters (arbitrary)*/;
}
ch = 0;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
nUscore = 0;
break;
default:
if(numberMode!=0 || !fossil_isalpha(ch)){
if(numberMode || !fossil_isalpha(ch)){
goto hashref_bailout;
}
nUscore = 0;
break;
}
if(ch) continue;
break;
}
if((end<3/* #. or some such */ && !numberMode)
|| end>size/*from truncated multi-byte char*/){
return 0;
}
if(numberMode>1){
/* Check for trailing part of #NNN.nnn... */
assert('.'==data[end]);
if(end<size-1 && fossil_isdigit(data[end+1])){
for(++end; end<size; ++end){
if(!fossil_isdigit(data[end])) break;
}
}
}
#if 0
fprintf(stderr,"?HASHREF length=%d: %.*s\n",
fprintf(stderr,"???HASHREF length=%d: %.*s\n",
(int)end, (int)end, data);
#endif
if(end<size){
/* Only match if we end at end of input or what "might" be the end
of a natural language grammar construct, e.g. period or
[semi]colon. */
switch(data[end]){
case '.':
HASHTAG_LEGAL_END:
break;
default:
goto hashref_bailout;
}
}
blob_init(&work, data + 1, end - 1);
rndr->make.tagspan(ob, &work, MKDT_HASHTAG, rndr->make.opaque);
rndr->make.tagspan(ob, &work,
numberMode ? MKDT_NUMTAG : MKDT_HASHTAG,
rndr->make.opaque);
return end;
hashref_bailout:
#if 0
fprintf(stderr,"BAILING HASHREF examined=%d:\n[%.*s] of\n[%.*s]\n",
(int)end, (int)end, data, (int)size, data);
#endif
#undef HASHTAG_LEGAL_END
|