Fossil SCM

Added optional filename parameter to output_text_with_line_numbers() so that it can add the language-X class to the CODE element.

stephan 2020-08-14 12:21 line-number-selection
Commit 0b3919f3e13218b0bad75a2a04e9d3ccd02c91a50933870f89ae13c265ea8b98
+2 -2
--- src/ajax.c
+++ src/ajax.c
@@ -130,12 +130,12 @@
130130
wiki_render_by_mimetype(pContent, zMime);
131131
break;
132132
default:{
133133
const char *zContent = blob_str(pContent);
134134
if(AJAX_PREVIEW_LINE_NUMBERS & flags){
135
- output_text_with_line_numbers(zContent,
136
- blob_size(pContent), "on");
135
+ output_text_with_line_numbers(zContent, blob_size(pContent),
136
+ zName, "on");
137137
}else{
138138
const char *zExt = strrchr(zName,'.');
139139
if(zExt && zExt[1]){
140140
CX("<pre><code class='language-%s'>%h</code></pre>",
141141
zExt+1, zContent);
142142
--- src/ajax.c
+++ src/ajax.c
@@ -130,12 +130,12 @@
130 wiki_render_by_mimetype(pContent, zMime);
131 break;
132 default:{
133 const char *zContent = blob_str(pContent);
134 if(AJAX_PREVIEW_LINE_NUMBERS & flags){
135 output_text_with_line_numbers(zContent,
136 blob_size(pContent), "on");
137 }else{
138 const char *zExt = strrchr(zName,'.');
139 if(zExt && zExt[1]){
140 CX("<pre><code class='language-%s'>%h</code></pre>",
141 zExt+1, zContent);
142
--- src/ajax.c
+++ src/ajax.c
@@ -130,12 +130,12 @@
130 wiki_render_by_mimetype(pContent, zMime);
131 break;
132 default:{
133 const char *zContent = blob_str(pContent);
134 if(AJAX_PREVIEW_LINE_NUMBERS & flags){
135 output_text_with_line_numbers(zContent, blob_size(pContent),
136 zName, "on");
137 }else{
138 const char *zExt = strrchr(zName,'.');
139 if(zExt && zExt[1]){
140 CX("<pre><code class='language-%s'>%h</code></pre>",
141 zExt+1, zContent);
142
+1 -1
--- src/attach.c
+++ src/attach.c
@@ -617,11 +617,11 @@
617617
const char *z;
618618
content_get(ridSrc, &attach);
619619
blob_to_utf8_no_bom(&attach, 0);
620620
z = blob_str(&attach);
621621
if( zLn ){
622
- output_text_with_line_numbers(z, blob_size(&attach), zLn);
622
+ output_text_with_line_numbers(z, blob_size(&attach), zName, zLn);
623623
}else{
624624
@ <pre>
625625
@ %h(z)
626626
@ </pre>
627627
}
628628
--- src/attach.c
+++ src/attach.c
@@ -617,11 +617,11 @@
617 const char *z;
618 content_get(ridSrc, &attach);
619 blob_to_utf8_no_bom(&attach, 0);
620 z = blob_str(&attach);
621 if( zLn ){
622 output_text_with_line_numbers(z, blob_size(&attach), zLn);
623 }else{
624 @ <pre>
625 @ %h(z)
626 @ </pre>
627 }
628
--- src/attach.c
+++ src/attach.c
@@ -617,11 +617,11 @@
617 const char *z;
618 content_get(ridSrc, &attach);
619 blob_to_utf8_no_bom(&attach, 0);
620 z = blob_str(&attach);
621 if( zLn ){
622 output_text_with_line_numbers(z, blob_size(&attach), zName, zLn);
623 }else{
624 @ <pre>
625 @ %h(z)
626 @ </pre>
627 }
628
+9
--- src/file.c
+++ src/file.c
@@ -2397,5 +2397,14 @@
23972397
changeCount);
23982398
}else{
23992399
fossil_print("Touched %d file(s)\n", changeCount);
24002400
}
24012401
}
2402
+
2403
+/*
2404
+** If zFileName is not NULL and contains a '.', this returns a pointer
2405
+** to the position after the final '.', else it returns NULL.
2406
+*/
2407
+const char * file_extension(const char *zFileName){
2408
+ const char * zExt = strrchr(zFileName, '.');
2409
+ return zExt ? &zExt[1] : 0;
2410
+}
24022411
--- src/file.c
+++ src/file.c
@@ -2397,5 +2397,14 @@
2397 changeCount);
2398 }else{
2399 fossil_print("Touched %d file(s)\n", changeCount);
2400 }
2401 }
 
 
 
 
 
 
 
 
 
2402
--- src/file.c
+++ src/file.c
@@ -2397,5 +2397,14 @@
2397 changeCount);
2398 }else{
2399 fossil_print("Touched %d file(s)\n", changeCount);
2400 }
2401 }
2402
2403 /*
2404 ** If zFileName is not NULL and contains a '.', this returns a pointer
2405 ** to the position after the final '.', else it returns NULL.
2406 */
2407 const char * file_extension(const char *zFileName){
2408 const char * zExt = strrchr(zFileName, '.');
2409 return zExt ? &zExt[1] : 0;
2410 }
2411
+17 -4
--- src/info.c
+++ src/info.c
@@ -2014,10 +2014,14 @@
20142014
20152015
/*
20162016
** The "z" argument is a string that contains the text of a source
20172017
** code file and nZ is its length in bytes. This routine appends that
20182018
** text to the HTTP reply with line numbering.
2019
+**
2020
+** zName is the content's file name, if any (it may be NULL). If that
2021
+** name contains a '.' then the part after the final '.' is used as
2022
+** the X part of a "language-X" CSS class on the generate CODE block.
20192023
**
20202024
** zLn is the ?ln= parameter for the HTTP query. If there is an argument,
20212025
** then highlight that line number and scroll to it once the page loads.
20222026
** If there are two line numbers, highlight the range of lines.
20232027
** Multiple ranges can be highlighed by adding additional line numbers
@@ -2024,17 +2028,19 @@
20242028
** separated by a non-digit character (also not one of [-,.]).
20252029
*/
20262030
void output_text_with_line_numbers(
20272031
const char *z,
20282032
int nZ,
2033
+ const char *zName,
20292034
const char *zLn
20302035
){
20312036
int iStart, iEnd; /* Start and end of region to highlight */
20322037
int n = 0; /* Current line number */
20332038
int i = 0; /* Loop index */
20342039
int iTop = 0; /* Scroll so that this line is on top of screen. */
20352040
int nLine = 0;
2041
+ const char *zExt = file_extension(zName);
20362042
Stmt q;
20372043
20382044
iStart = iEnd = atoi(zLn);
20392045
db_multi_exec(
20402046
"CREATE TEMP TABLE lnos(iStart INTEGER PRIMARY KEY, iEnd INTEGER)");
@@ -2066,11 +2072,16 @@
20662072
CX("<table class='numbered-lines'><tbody><tr><td>");
20672073
count_lines(z, nZ, &nLine);
20682074
for(i=0; i < nLine; ++i){
20692075
CX("<span>%6d</span>", i+1);
20702076
}
2071
- CX("</td><td><pre><code>");
2077
+ CX("</td><td><pre>");
2078
+ if(zExt && *zExt){
2079
+ CX("<code class='language-%h'>",zExt);
2080
+ }else{
2081
+ CX("<code>");
2082
+ }
20722083
assert(!n);
20732084
while( z[0] ){
20742085
n++;
20752086
db_prepare(&q,
20762087
"SELECT min(iStart), max(iEnd) FROM lnos"
@@ -2123,11 +2134,12 @@
21232134
db_find_and_open_repository(0,0);
21242135
zFilename = g.argv[2];
21252136
fossil_print("%s %s\n", zFilename, zLn);
21262137
21272138
blob_read_from_file(&content, zFilename, ExtFILE);
2128
- output_text_with_line_numbers(blob_str(&content), blob_size(&content), zLn);
2139
+ output_text_with_line_numbers(blob_str(&content), blob_size(&content),
2140
+ zFilename, zLn);
21292141
blob_reset(&content);
21302142
fossil_print("%b\n", cgi_output_blob());
21312143
}
21322144
21332145
/*
@@ -2430,13 +2442,14 @@
24302442
zFileName = db_text(0,
24312443
"SELECT name FROM mlink, filename"
24322444
" WHERE filename.fnid=mlink.fnid"
24332445
" AND mlink.fid=%d",
24342446
rid);
2435
- zExt = zFileName ? strrchr(zFileName, '.') : 0;
2447
+ zExt = file_extension(zFileName);
24362448
if( zLn ){
2437
- output_text_with_line_numbers(z, blob_size(&content), zLn);
2449
+ output_text_with_line_numbers(z, blob_size(&content),
2450
+ zFileName, zLn);
24382451
}else if( zExt && zExt[1] ){
24392452
@ <pre>
24402453
@ <code class="language-%s(zExt+1)">%h(z)</code>
24412454
@ </pre>
24422455
}else{
24432456
--- src/info.c
+++ src/info.c
@@ -2014,10 +2014,14 @@
2014
2015 /*
2016 ** The "z" argument is a string that contains the text of a source
2017 ** code file and nZ is its length in bytes. This routine appends that
2018 ** text to the HTTP reply with line numbering.
 
 
 
 
2019 **
2020 ** zLn is the ?ln= parameter for the HTTP query. If there is an argument,
2021 ** then highlight that line number and scroll to it once the page loads.
2022 ** If there are two line numbers, highlight the range of lines.
2023 ** Multiple ranges can be highlighed by adding additional line numbers
@@ -2024,17 +2028,19 @@
2024 ** separated by a non-digit character (also not one of [-,.]).
2025 */
2026 void output_text_with_line_numbers(
2027 const char *z,
2028 int nZ,
 
2029 const char *zLn
2030 ){
2031 int iStart, iEnd; /* Start and end of region to highlight */
2032 int n = 0; /* Current line number */
2033 int i = 0; /* Loop index */
2034 int iTop = 0; /* Scroll so that this line is on top of screen. */
2035 int nLine = 0;
 
2036 Stmt q;
2037
2038 iStart = iEnd = atoi(zLn);
2039 db_multi_exec(
2040 "CREATE TEMP TABLE lnos(iStart INTEGER PRIMARY KEY, iEnd INTEGER)");
@@ -2066,11 +2072,16 @@
2066 CX("<table class='numbered-lines'><tbody><tr><td>");
2067 count_lines(z, nZ, &nLine);
2068 for(i=0; i < nLine; ++i){
2069 CX("<span>%6d</span>", i+1);
2070 }
2071 CX("</td><td><pre><code>");
 
 
 
 
 
2072 assert(!n);
2073 while( z[0] ){
2074 n++;
2075 db_prepare(&q,
2076 "SELECT min(iStart), max(iEnd) FROM lnos"
@@ -2123,11 +2134,12 @@
2123 db_find_and_open_repository(0,0);
2124 zFilename = g.argv[2];
2125 fossil_print("%s %s\n", zFilename, zLn);
2126
2127 blob_read_from_file(&content, zFilename, ExtFILE);
2128 output_text_with_line_numbers(blob_str(&content), blob_size(&content), zLn);
 
2129 blob_reset(&content);
2130 fossil_print("%b\n", cgi_output_blob());
2131 }
2132
2133 /*
@@ -2430,13 +2442,14 @@
2430 zFileName = db_text(0,
2431 "SELECT name FROM mlink, filename"
2432 " WHERE filename.fnid=mlink.fnid"
2433 " AND mlink.fid=%d",
2434 rid);
2435 zExt = zFileName ? strrchr(zFileName, '.') : 0;
2436 if( zLn ){
2437 output_text_with_line_numbers(z, blob_size(&content), zLn);
 
2438 }else if( zExt && zExt[1] ){
2439 @ <pre>
2440 @ <code class="language-%s(zExt+1)">%h(z)</code>
2441 @ </pre>
2442 }else{
2443
--- src/info.c
+++ src/info.c
@@ -2014,10 +2014,14 @@
2014
2015 /*
2016 ** The "z" argument is a string that contains the text of a source
2017 ** code file and nZ is its length in bytes. This routine appends that
2018 ** text to the HTTP reply with line numbering.
2019 **
2020 ** zName is the content's file name, if any (it may be NULL). If that
2021 ** name contains a '.' then the part after the final '.' is used as
2022 ** the X part of a "language-X" CSS class on the generate CODE block.
2023 **
2024 ** zLn is the ?ln= parameter for the HTTP query. If there is an argument,
2025 ** then highlight that line number and scroll to it once the page loads.
2026 ** If there are two line numbers, highlight the range of lines.
2027 ** Multiple ranges can be highlighed by adding additional line numbers
@@ -2024,17 +2028,19 @@
2028 ** separated by a non-digit character (also not one of [-,.]).
2029 */
2030 void output_text_with_line_numbers(
2031 const char *z,
2032 int nZ,
2033 const char *zName,
2034 const char *zLn
2035 ){
2036 int iStart, iEnd; /* Start and end of region to highlight */
2037 int n = 0; /* Current line number */
2038 int i = 0; /* Loop index */
2039 int iTop = 0; /* Scroll so that this line is on top of screen. */
2040 int nLine = 0;
2041 const char *zExt = file_extension(zName);
2042 Stmt q;
2043
2044 iStart = iEnd = atoi(zLn);
2045 db_multi_exec(
2046 "CREATE TEMP TABLE lnos(iStart INTEGER PRIMARY KEY, iEnd INTEGER)");
@@ -2066,11 +2072,16 @@
2072 CX("<table class='numbered-lines'><tbody><tr><td>");
2073 count_lines(z, nZ, &nLine);
2074 for(i=0; i < nLine; ++i){
2075 CX("<span>%6d</span>", i+1);
2076 }
2077 CX("</td><td><pre>");
2078 if(zExt && *zExt){
2079 CX("<code class='language-%h'>",zExt);
2080 }else{
2081 CX("<code>");
2082 }
2083 assert(!n);
2084 while( z[0] ){
2085 n++;
2086 db_prepare(&q,
2087 "SELECT min(iStart), max(iEnd) FROM lnos"
@@ -2123,11 +2134,12 @@
2134 db_find_and_open_repository(0,0);
2135 zFilename = g.argv[2];
2136 fossil_print("%s %s\n", zFilename, zLn);
2137
2138 blob_read_from_file(&content, zFilename, ExtFILE);
2139 output_text_with_line_numbers(blob_str(&content), blob_size(&content),
2140 zFilename, zLn);
2141 blob_reset(&content);
2142 fossil_print("%b\n", cgi_output_blob());
2143 }
2144
2145 /*
@@ -2430,13 +2442,14 @@
2442 zFileName = db_text(0,
2443 "SELECT name FROM mlink, filename"
2444 " WHERE filename.fnid=mlink.fnid"
2445 " AND mlink.fid=%d",
2446 rid);
2447 zExt = file_extension(zFileName);
2448 if( zLn ){
2449 output_text_with_line_numbers(z, blob_size(&content),
2450 zFileName, zLn);
2451 }else if( zExt && zExt[1] ){
2452 @ <pre>
2453 @ <code class="language-%s(zExt+1)">%h(z)</code>
2454 @ </pre>
2455 }else{
2456

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button