Fossil

Diff
Login

Differences From Artifact [6d6ceb9be3]:

To Artifact [78bde668b2]:


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
     search_title_sqlfunc, 0, 0);
  sqlite3_create_function(db, "body", 3, enc, 0,
     search_body_sqlfunc, 0, 0);
  sqlite3_create_function(db, "urlencode", 1, enc, 0,
     search_urlencode_sqlfunc, 0, 0);
}

/*
** The pSnip input contains snippet text from a search formatted
** as HTML.  Attempt to make that text more readable on a TTY.
**
** If nTty is positive, use ANSI escape codes "\e[Nm" where N is nTty
** to highly marked text.
*/
void search_snippet_to_plaintext(Blob *pSnip, int nTty){
  char *zSnip;
  unsigned int j, k;

  zSnip = pSnip->aData;
  for(j=k=0; j<pSnip->nUsed; j++){
    char c = zSnip[j];
    if( c=='<' ){
      if( memcmp(&zSnip[j],"<mark>",6)==0 ){
        if( nTty ){
          zSnip[k++] = 0x1b;
          zSnip[k++] = '[';
          if( nTty>=10 ) zSnip[k++] = (nTty/10)%10 + '0';
          zSnip[k++] = nTty%10 + '0';
          zSnip[k++] = 'm';
        }
        j += 5;
      }else if( memcmp(&zSnip[j],"</mark>",7)==0 ){
        if( nTty ){
          zSnip[k++] = 0x1b;
          zSnip[k++] = '[';
          zSnip[k++] = '0';
          zSnip[k++] = 'm';
        }
        j += 6;
      }else{
        zSnip[k++] = zSnip[j];
      }
    }else if( fossil_isspace(c) ){
      zSnip[k++] = ' ';
      while( fossil_isspace(zSnip[j+1]) ) j++;
    }else if( c=='&' ){
      if( zSnip[j+1]=='#' && fossil_isdigit(zSnip[j+2]) ){
        int n = 3;
        int x = zSnip[j+2] - '0';
        if( fossil_isdigit(zSnip[j+3]) ){
          x = x*10 + zSnip[j+3] - '0';
          n++;
          if( fossil_isdigit(zSnip[j+4]) ){
            x = x*10 + zSnip[j+4] - '0';
            n++;
          }
        }
        if( zSnip[j+n]==';' ){
          zSnip[k++] = (char)x;
          j += n;
        }else{
          zSnip[k++] = c;
        }
      }else if( memcmp(&zSnip[j],"&lt;",4)==0 ){
        zSnip[k++] = '<';
        j += 3;
      }else if( memcmp(&zSnip[j],"&gt;",4)==0 ){
        zSnip[k++] = '>';
        j += 3;
      }else if( memcmp(&zSnip[j],"&quot;",6)==0 ){
        zSnip[k++] = '"';
        j += 5;
      }else if( memcmp(&zSnip[j],"&amp;",5)==0 ){
        zSnip[k++] = '&';
        j += 4;
      }else{
        zSnip[k++] = c;
      }
    }else if( c=='%' && strncmp(&zSnip[j],"%fossil",7)==0 ){
      /* no-op */
    }else if( (c=='[' || c==']') && zSnip[j+1]==c ){
      j++;
    }else{
      zSnip[k++] = c;
    }
  }
  zSnip[k] = 0;
  pSnip->nUsed = k;
}

/*
** Testing the search function.
**
** COMMAND: search*
**
** Usage: %fossil search [OPTIONS] PATTERN...
**







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







557
558
559
560
561
562
563



















































































564
565
566
567
568
569
570
     search_title_sqlfunc, 0, 0);
  sqlite3_create_function(db, "body", 3, enc, 0,
     search_body_sqlfunc, 0, 0);
  sqlite3_create_function(db, "urlencode", 1, enc, 0,
     search_urlencode_sqlfunc, 0, 0);
}




















































































/*
** Testing the search function.
**
** COMMAND: search*
**
** Usage: %fossil search [OPTIONS] PATTERN...
**
803
804
805
806
807
808
809
810
811
812
813
814
815
816

817

818


819
820
821
822
823
824
825
      }
    }
    db_prepare(&q, "SELECT snip, label, score, id, date"
                   "  FROM x"
                   " ORDER BY score DESC, date DESC;");
    blob_init(&com, 0, 0);
    blob_init(&snip, 0, 0);
    if( width<0 ) width = 80;
    while( db_step(&q)==SQLITE_ROW ){
      const char *zSnippet = db_column_text(&q, 0);
      const char *zLabel = db_column_text(&q, 1);
      const char *zDate = db_column_text(&q, 4);
      const char *zScore = db_column_text(&q, 2);
      const char *zId = db_column_text(&q, 3);

      blob_appendf(&snip, "%s", zSnippet);

      search_snippet_to_plaintext(&snip, nTty);


      blob_appendf(&com, "%s\n%s\n%s", zLabel, blob_str(&snip), zDate);
      if( bDebug ){
        blob_appendf(&com," score: %s id: %s", zScore, zId);
      }
      comment_print(blob_str(&com), 0, 5, width,
            COMMENT_PRINT_TRIM_CRLF |
            COMMENT_PRINT_WORD_BREAK |







|






>

>
|
>
>







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
      }
    }
    db_prepare(&q, "SELECT snip, label, score, id, date"
                   "  FROM x"
                   " ORDER BY score DESC, date DESC;");
    blob_init(&com, 0, 0);
    blob_init(&snip, 0, 0);
    if( width<0 ) width = terminal_get_width(80);
    while( db_step(&q)==SQLITE_ROW ){
      const char *zSnippet = db_column_text(&q, 0);
      const char *zLabel = db_column_text(&q, 1);
      const char *zDate = db_column_text(&q, 4);
      const char *zScore = db_column_text(&q, 2);
      const char *zId = db_column_text(&q, 3);
      char *zOrig;
      blob_appendf(&snip, "%s", zSnippet);
      zOrig = blob_materialize(&snip);
      blob_init(&snip, 0, 0);
      html_to_plaintext(zOrig, &snip, (nTty ? HTOT_VT100 : 0)|HTOT_NO_WS);
      fossil_free(zOrig);
      blob_appendf(&com, "%s\n%s\n%s", zLabel, blob_str(&snip), zDate);
      if( bDebug ){
        blob_appendf(&com," score: %s id: %s", zScore, zId);
      }
      comment_print(blob_str(&com), 0, 5, width,
            COMMENT_PRINT_TRIM_CRLF |
            COMMENT_PRINT_WORD_BREAK |
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
        wiki_convert(&tail, &html, 0);
        blob_reset(&tail);
      }else{
        blob_append(pOut, "\n", 1);
        wiki_convert(pIn, &html, 0);
      }
    }
    html_to_plaintext(blob_str(&html), pOut);
  }else if( fossil_strcmp(zMimetype,"text/x-markdown")==0 ){
    markdown_to_html(pIn, blob_size(&title) ? NULL : &title, &html);
  }else if( fossil_strcmp(zMimetype,"text/html")==0 ){
    if( blob_size(&title)==0 ) doc_is_embedded_html(pIn, &title);
    pHtml = pIn;
  }
  blob_appendf(pOut, "%s\n", blob_str(&title));
  if( blob_size(pHtml) ){
    html_to_plaintext(blob_str(pHtml), pOut);
  }else{
    blob_append(pOut, blob_buffer(pIn), blob_size(pIn));
  }
  blob_reset(&html);
  blob_reset(&title);
}








|








|







1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
        wiki_convert(&tail, &html, 0);
        blob_reset(&tail);
      }else{
        blob_append(pOut, "\n", 1);
        wiki_convert(pIn, &html, 0);
      }
    }
    html_to_plaintext(blob_str(&html), pOut, 0);
  }else if( fossil_strcmp(zMimetype,"text/x-markdown")==0 ){
    markdown_to_html(pIn, blob_size(&title) ? NULL : &title, &html);
  }else if( fossil_strcmp(zMimetype,"text/html")==0 ){
    if( blob_size(&title)==0 ) doc_is_embedded_html(pIn, &title);
    pHtml = pIn;
  }
  blob_appendf(pOut, "%s\n", blob_str(&title));
  if( blob_size(pHtml) ){
    html_to_plaintext(blob_str(pHtml), pOut, 0);
  }else{
    blob_append(pOut, blob_buffer(pIn), blob_size(pIn));
  }
  blob_reset(&html);
  blob_reset(&title);
}