Team Ai
Datasetpublic

hschumann2/TempleOS-Source-Code

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes847downloads
TOSToHtml.txt504 linesDownload Raw Back to TOS
1 2//This is a customized version of ::/Demo/ToHtmlToTXTDemo/ToHtml.HC.3//Diff4 5#help_index "DolDoc/Conversion"6//See ::/Doc/Credits.DD.7 8#define DOCET_UNDEF_COLOR       0x1000000009#define DOCET_LINK_UNDERLINE    0x20000000010U0 HtmlPutS(CDoc *doc,I64 u32_attr,I64 *_old_u32_attr,Bool underline_update,11        U8 *st,I64 *_col,U8 *style_bitmap=NULL)12{13  U8 *ch,*ptr;14 15  u32_attr&=0xFFFFFF00;16  if (u32_attr&DOCET_INVERT)17    u32_attr.u8[1]=(u32_attr.u8[1]&15)<<4|u32_attr.u8[1]>>4;18 19  if (underline_update &&20        !(u32_attr&DOCET_UNDERLINE) && *_old_u32_attr&DOCET_UNDERLINE) {21    if (!(*_old_u32_attr&DOCET_LINK_UNDERLINE))22      DocPrint(doc,"</u>");23    *_old_u32_attr&=~DOCET_LINK_UNDERLINE;24  }25  if (!(u32_attr&DOCET_BLINK) && *_old_u32_attr&DOCET_BLINK)26    DocPrint(doc,"</blink>");27 28  if (u32_attr&0xFF00!=*_old_u32_attr&0xFF00) {29    if (!(*_old_u32_attr&DOCET_UNDEF_COLOR))30      DocPrint(doc,"</span>");31    DocPrint(doc,"<span class=c%02X>",u32_attr.u8[1]);32    if (style_bitmap)33      LBts(style_bitmap,u32_attr.u8[1]);34  }35  if (u32_attr&DOCET_BLINK && !(*_old_u32_attr&DOCET_BLINK))36    DocPrint(doc,"<blink>");37  if (underline_update) {38    if (u32_attr&DOCET_UNDERLINE && !(*_old_u32_attr&DOCET_UNDERLINE))39      DocPrint(doc,"<u>");40  } else //Keep same underline status.41    u32_attr=u32_attr&~DOCET_UNDERLINE | *_old_u32_attr&DOCET_UNDERLINE;42 43  *_old_u32_attr=u32_attr | *_old_u32_attr&DOCET_LINK_UNDERLINE;44 45  while (ch=*st++) {46    switch (ch) {47      case '\t':48        do {49          DocPutKey(doc,CH_SPACE,0);50          *_col=*_col+1;51        } while (*_col&7);52        break;53 54      start:55        case 'pi':      ptr="pi";       break;56        case 'theta':   ptr="theta";    break;57        case 'phi':     ptr="phi";      break;58        case 'omega':   ptr="omega";    break;59        case 'inf':     ptr="inf";      break;60        case 'u':       ptr="u";        break;61      end:62        DocPrint(doc,ptr);63        *_col=*_col+StrLen(ptr);64        break;65 66      start:67        case CH_SHIFT_SPACE: ptr=" ";   break;68        case '&':       ptr="&";    break;69        case '<':       ptr="<";     break;70        case '>':       ptr=">";     break;71        case '"':       ptr=""";   break;72      end:73        DocPrint(doc,ptr);74        *_col=*_col+1;75        break;76 77      default:78        if (CH_SPACE<=ch<0x7F || ch=='\n')79          DocPutKey(doc,ch,0);80        else81          DocPrint(doc,".");82        *_col=*_col+1;83    }84  }85}86 87U8 *TOSLinkCvt2(U8 *filename,I64 line_num)88{// ::/  --> /Wb/89//Make your own LinkCvt routine90  U8 *res=NULL,*st,*st2;91  if (filename) {92    st2=TOSBlogAbs(filename);93    st=ExtChg(st2,"html");94    Free(st2);95    if (st && StrLen(st)>3 && !StrNCmp(st+1,":/",2))96      res=MStrPrint(PERSONAL_WEB "/%s#l%d",st+3,line_num);97    Free(st);98  }99  return res;100}101 102U8 *URLBibleGateway(U8 *src)103{104  U8 buf[STR_LEN],*dst=buf;105  if (!MemCmp(src,"BF:",3))106    src+=3;107  while (*src) {108    if (*src==CH_SPACE || *src==',')109      *dst++='+';110    else111      *dst++=*src;112    src++;113  }114  *dst=0;115  if (StrOcc(buf,'-'))116    return MStrPrint(117          "http://www.biblegateway.com/passage/?search=%s&version=NIV",buf);118  else119    return MStrPrint(120          "http://www.biblegateway.com/verse/en/%s",buf);121}122 123U8 *TOSLinkCvt1(U8 *link_st)124{125  static CDoc *bible=NULL;126  static I64 locks=0;127  U8 *res=NULL,*filename,*needle;128  I64 i,num;129  if (link_st) {130    switch (i=EdLinkCvt(link_st,&filename,&needle,&num)) {131      case LK_FILE_LINE:132      case LK_PLAIN_LINE:133      case LK_FILE:134        res=TOSLinkCvt2(filename,num);135        break;136      case -1:137      case LK_DEF:138      case LK_HELP_INDEX:139      case LK_DOC:140      case LK_DOC_ANCHOR:141      case LK_DOC_FIND:142      case LK_DOC_LINE:143        break;144      case LK_BIBLE_FIND:145        while (LBts(&locks,0))146          Yield;147        if (!bible)148          bible=Adam("DocRead(\"%s\");",filename);149        if (DocFind(bible,num,needle))150          res=URLBibleGateway(link_st);151        LBtr(&locks,0);152        break;153      default:154        if (DocFileEd(i,filename,needle,&num,EDF_UNCOLLAPSE|EDF_BAIL))155          res=TOSLinkCvt2(filename,num);156    }157    Free(filename);158    Free(needle);159  }160  return res;161}162 163 164public CDoc *Doc2Html(CDoc *doc_in,U8 *html_header=NULL,U8 *body_header=NULL,165        U8 *body_footer=NULL,U8 *html_footer=NULL,Bool line_anchors=TRUE,166        U8 (*link_cvt)(U8 *link_st)=&TOSLinkCvt1,Bool line_nums=FALSE)167{//Cvt DolDocdoc to HTML file.168  CDocEntry *doc_e,*style,*doc_e2;169  I64 i,y,old_y=I64_MIN,col,old_u32_attr=DOCET_UNDEF_COLOR,old_attr,digits;170  U32 *hl,*src;171  U8 *st,st_2[2],*link_st,*style_bitmap=CAlloc(256/8);172  CBGR48 p[COLORS_NUM];173  GrPaletteGet(p);174  CDoc *doc_out=DocNew;175  Bool unlock_doc_in=DocLock(doc_in),no_bwd;176 177  old_attr=doc_in->win_task->text_attr;178  doc_in->win_task->text_attr=DOC_ATTR_DFT_TEXT;179 180  for (i=0xF0;i<=0xFF;i++)181    LBts(style_bitmap,i);182 183  DocRecalc(doc_in,RECALCt_NORMAL|RECALCF_TO_HTML);184  digits=Log10(doc_in->head.last->y+1)+1;185 186  st_2[0]=0; st_2[1]=0;187  doc_out->flags|=DOCF_PLAIN_TEXT|DOCF_NO_CURSOR;188 189  if (!html_header) html_header=190          "<!DOCTYPE HTML>\n"191          "<html>\n"192          "<head>\n"193          "<meta http-equiv=\"Content-Type\" "194          "content=\"text/html;charset=US-ASCII\">\n"195          "<meta name=\"generator\" content=\"TempleOS V5.03\">\n";196  if (!body_header) body_header=197          "<body>\n"198          "<pre style=\"font-family:courier;font-size:10pt\">\n";199  if (!body_footer) body_footer=200          "</pre></body>\n</html>\n";201  if (!html_footer) html_footer=202          "</html>\n";203 204  DocPrint(doc_out,"%s",html_header);205 206  DocPrint(doc_out,"<style type=\"text/css\">\n");207  style=doc_out->cur_entry->last;208  DocPrint(doc_out,209        "</style>\n"210        "</head>\n");211  DocPrint(doc_out,"%s",body_header);212 213  doc_e=doc_in->head.next;214  col=doc_e->x;215  y=doc_e->y;216  while (doc_e!=doc_in) {217    if (!(doc_e->de_flags&DOCEF_SKIP)) {218      if (y!=old_y) {219        if (line_anchors)220          DocPrint(doc_out,"<a name=\"l%d\"></a>",y+1);221        if (line_nums)222          DocPrint(doc_out,"%0*d ",digits,y+1);223        old_y=y;224      }225      while (y<doc_e->y) {226        HtmlPutS(doc_out,doc_e->settings.final_u32_attr,&old_u32_attr,FALSE,227              "\n",&col,style_bitmap);228        if (++y!=old_y) {229          if (line_anchors)230            DocPrint(doc_out,"<a name=\"l%d\"></a>",y+1);231          if (line_nums)232            DocPrint(doc_out,"%0*d ",digits,y+1);233          old_y=y;234        }235        col=0;236      }237 238      no_bwd=TRUE;239      doc_e2=doc_e->next;240      while (doc_e2!=doc_in && doc_e2->y==doc_e->y) {241        if (doc_e2->x<doc_e->x) {242          no_bwd=FALSE;243          break;244        }245        doc_e2=doc_e2->next;246      }247 248      if (no_bwd)249        while (col<doc_e->x)250          HtmlPutS(doc_out,doc_e->settings.final_u32_attr&~DOCET_UNDERLINE,251                &old_u32_attr,TRUE," ",&col,style_bitmap);252 253      link_st=NULL;254      if (doc_e->de_flags&DOCEF_HTML_LINK)255        link_st=StrNew(doc_e->html_link);256      else if (st=DocEntryLink(doc_in,doc_e)) {257        link_st=link_cvt(st);258        Free(st);259      }260 261      if (link_st) {262        if (old_u32_attr&DOCET_UNDERLINE &&!(old_u32_attr&DOCET_LINK_UNDERLINE))263          DocPrint(doc_out,"</u>");264        if (old_u32_attr&DOCET_BLINK)265          DocPrint(doc_out,"</blink>");266        if (!(old_u32_attr&DOCET_UNDEF_COLOR))267          DocPrint(doc_out,"</span>");268        old_u32_attr=DOCET_UNDEF_COLOR|DOCET_UNDERLINE|DOCET_LINK_UNDERLINE;269        DocPrint(doc_out,"<a href=\"%s\">",link_st);270      }271 272      switch (doc_e->type_u8) {273        case DOCT_TEXT:274          if (doc_e->de_flags&DOCEF_HIGHLIGHT) {275            if (doc_e->last==doc_in)276              MemCpy(&doc_e->settings,&doc_in->settings_head,277                    sizeof(CDocSettings));278            else279              MemCpy(&doc_e->settings,&doc_e->last->settings,280                    sizeof(CDocSettings));281            src=hl=DocHighlight(doc_e,doc_e->tag,StrLen(doc_e->tag),282                  doc_e->type&0xFF00);283            while (*src) {284              st_2[0]=*src&0xFF;285              HtmlPutS(doc_out,*src++,&old_u32_attr,TRUE,286                    st_2,&col,style_bitmap);287            }288            Free(hl);289          } else {290            if (doc_e->de_flags&DOCEF_CENTER_X && //Nonstandard291                  doc_e->settings.final_u32_attr&0xF00==PURPLE<<8)292              DocPrint(doc_out,"<h1><center>");293            HtmlPutS(doc_out,doc_e->settings.final_u32_attr,294                  &old_u32_attr,TRUE,doc_e->tag,&col,style_bitmap);295            if (doc_e->de_flags&DOCEF_CENTER_X && //Nonstandard296                  doc_e->settings.final_u32_attr&0xF00==PURPLE<<8)297              DocPrint(doc_out,"</center></h1>");298          }299          break;300        case DOCT_TAB:301          HtmlPutS(doc_out,doc_e->settings.final_u32_attr,&old_u32_attr,TRUE,302                "\t",&col,style_bitmap);303          break;304        case DOCT_HTML_CODE:305          if (old_u32_attr&DOCET_UNDERLINE &&306                !(old_u32_attr&DOCET_LINK_UNDERLINE))307            DocPrint(doc_out,"</u>");308          if (old_u32_attr&DOCET_BLINK)309            DocPrint(doc_out,"</blink>");310          if (!(old_u32_attr&DOCET_UNDEF_COLOR))311            DocPrint(doc_out,"</span>");312          old_u32_attr=DOCET_UNDEF_COLOR;313          DocPrint(doc_out,"%s",doc_e->tag);314          break;315        case DOCT_SPRITE:316          HtmlPutS(doc_out,doc_e->settings.final_u32_attr,317                &old_u32_attr,TRUE,doc_e->tag,&col,style_bitmap);318          HtmlPutS(doc_out,doc_e->settings.final_u32_attr,319                &old_u32_attr,TRUE,"/* Graphics Not Rendered in HTML */",320                &col,style_bitmap);321          break;322        default:323          if (doc_e->de_flags&DOCEF_TAG)324            HtmlPutS(doc_out,doc_e->settings.final_u32_attr,325                  &old_u32_attr,TRUE,doc_e->tag,&col,style_bitmap);326      }327      if (link_st) {328        if (old_u32_attr&DOCET_UNDERLINE &&!(old_u32_attr&DOCET_LINK_UNDERLINE))329          DocPrint(doc_out,"</u>");330        if (old_u32_attr&DOCET_BLINK)331          DocPrint(doc_out,"</blink>");332        if (!(old_u32_attr&DOCET_UNDEF_COLOR))333          DocPrint(doc_out,"</span>");334        DocPrint(doc_out,"</a>");335        old_u32_attr=DOCET_UNDEF_COLOR|DOCET_UNDERLINE|DOCET_LINK_UNDERLINE;336        Free(link_st);337      }338    }339    doc_e=doc_e->next;340  }341  while (y++<doc_e->y) {342    HtmlPutS(doc_out,doc_e->settings.final_u32_attr,343          &old_u32_attr,TRUE,"\n",&col,style_bitmap);344    col=0;345  }346  if (old_u32_attr&DOCET_UNDERLINE && !(old_u32_attr&DOCET_LINK_UNDERLINE))347    DocPrint(doc_out,"</u>");348  if (old_u32_attr&DOCET_BLINK)349    DocPrint(doc_out,"</blink>");350  if (!(old_u32_attr&DOCET_UNDEF_COLOR))351    DocPrint(doc_out,"</span>");352  old_u32_attr=DOCET_UNDEF_COLOR;353  DocPrint(doc_out,"%s",body_footer);354  DocPrint(doc_out,"%s",html_footer);355 356  doc_out->cur_entry=style->next;357  for (i=0;i<256;i++)358    if (Bt(style_bitmap,i))359      DocPrint(doc_out,360            ".c%02X{color:#%02x%02x%02x;background-color:#%02x%02x%02x;}\n",361            i,p[i&15].r>>8,p[i&15].g>>8,p[i&15].b>>8,362            p[i/16].r>>8,p[i/16].g>>8,p[i/16].b>>8);363  doc_out->cur_entry=&doc_out->head;364  DocRecalc(doc_out);365 366  doc_in->win_task->text_attr=old_attr;367 368  if (unlock_doc_in)369    DocUnlock(doc_in);370  return doc_out;371}372 373#help_index "Cmd Line (Typically);DolDoc/Conversion;DolDoc/Cmd Line (Typically)"374public U0 TOSToHtml(U8 *_in_name,U8 *_out_name=NULL,U8 *html_header=NULL,375        U8 *body_header=NULL,U8 *body_footer=NULL,U8 *html_footer=NULL,376        I64 width=80,Bool line_anchors=TRUE,377        U8 (*link_cvt)(U8 *link_st)=&TOSLinkCvt1,Bool line_nums=FALSE)378{//Convert DolDocfile to HTML.379//Supply your own link_cvt routine.380  U8 *in_name,*out_name;381  CDoc *doc_in,*doc_out;382 383  SettingsPush; //See SettingsPush384  WinHorz(0,width-1); //Sets doc width for word wrap.385 386  in_name=ExtDft(_in_name,"HC.Z");387  if (_out_name)388    out_name=ExtDft(_out_name,"html");389  else390    out_name=ExtChg(_in_name,"html");391 392  doc_in=DocRead(in_name);393  DocTop(doc_in);394  DocPrint(doc_in,"$BD,LTCYAN$");       //Nonstandard395  doc_out=Doc2Html(doc_in,html_header,body_header,body_footer,html_footer,396        line_anchors,link_cvt,line_nums);397  StrCpy(&doc_out->filename.name,out_name);398 399  SettingsPop;400 401  DocWrite(doc_out);402  DocDel(doc_in);403  DocDel(doc_out);404  Free(in_name);405  Free(out_name);406}407 408#help_index "Misc/TOS"409U0 TOSWebFile(U8 *name,Bool line_nums=FALSE)410{411  U8 *name2;412  CDirEntry de;413  Bool line_anchors;414  if (FileFind(name,&de)) {415    Free(de.full_name);416    "File:%s\n",name;417    Collapse(name,FALSE);418    Touch(name,"",,de.datetime);419    name2=ExtChg(name,"html");420 421    if (StrMatch(BLOG_DST,name) || StrMatch(DAILY_DST,name))422      line_anchors=FALSE;423    else424      line_anchors=TRUE;425 426    TOSToHtml(name,name2,427          "<!DOCTYPE HTML>\n"428          "<html style=\"background-color:#FFFFFF;\">\n"429          "<head>\n"430          "<meta http-equiv=\"Content-Type\" "431          "content=\"text/html;charset=US-ASCII\">\n"432          "<title>The Temple Operating System</title>\n"433          "<meta name=\"keywords\" "434          "content=\"Operating System,64-Bit,64 Bit,"435          "Temple,OS,TempleOS,Free,Open Source,Public Domain,x86_64\">\n"436          "<meta name=\"generator\" content=\"TempleOS V5.03\">\n",437 438          "<body style=\"background-color:#55FFFF; border-style:solid;439          border-width:8px; border-color:#0000AA;440          margin-left:auto; margin-right:auto; width:800px; \">\n"441          "<pre style=\"font-family:courier;font-size:10pt\">\n"442          "<div style=\"margin-left:auto; margin-right:auto;"443          " width: 640px;\">\n",444          "</div></pre></body>\n",445          "</html>\n",,line_anchors,,line_nums);446    Touch(name2,"",,de.datetime);447    Free(name2);448  }449}450 451U0 FreshenWebDir(U8 *srcdir,Bool line_nums=FALSE)452{//No recurse453  CDirEntry *tmpde,*tmpde1;454  U8 *dstdir,*src,*dst;455  dstdir=TOSBlogAbs(srcdir);456 457  src=MStrPrint("D:/Wb%s",dstdir+2);458  DirMk(src);459  Del(src);460  Free(src);461 462  src=MStrPrint("C:%s/*",srcdir);463  dst=MStrPrint("D:/Wb%s",dstdir+2);464  Copy(src,dst);465  Free(src);466  Free(dst);467 468  src=MStrPrint("D:/Wb%s/*",dstdir+2);469  tmpde=tmpde1=FilesFind(src,FUF_JUST_FILES|FUF_JUST_TXT|FUF_CLUS_ORDER);470  while (tmpde) {471    TOSWebFile(tmpde->full_name,line_nums);472    tmpde=tmpde->next;473  }474  DirTreeDel(tmpde1);475 476  Free(dstdir);477}478 479public U0 FreshenSite()480{//Regen html files in D:/Wb481  CDirEntry *tmpde,*tmpde1;482  DocClear;483  Cd("D:/Wb");484  DelTree("D:/Wb/Home/Books1");485  DelTree("D:/Wb/Home/Books2");486  CopyTree("D:/Wb/Home/" INS_REG_PERSONAL_INITIALS "/TOSBlog",487        "D:/Wb/Home/Web/" INS_REG_PERSONAL_INITIALS);488  DelTree("D:/Wb/Home/ALM");489  DelTree("D:/Wb/Home/TAD");490  Del("D:/Wb/Home/*");491  Touch("D:/Wb/Home/Web/index.DD.Z","");492  Touch("D:/Wb/Home/Web/DownloadOS.DD.Z","");493  Touch("D:/Wb/Home/Web/TempleOS.DD.Z","");494  tmpde=tmpde1=FilesFind("*",495        FUF_RECURSE|FUF_JUST_FILES|FUF_JUST_TXT|FUF_CLUS_ORDER);496  while (tmpde) {497    TOSWebFile(tmpde->full_name);498    tmpde=tmpde->next;499  }500  DirTreeDel(tmpde1);501  FreshenWebDir("/Home/Sup1/Sup1Words",TRUE);502  Drv('C');503}504