mk_html.php

<?php

//============================================================
//    mk_figure(class Figure)
//    
//============================================================
class Figure{
  private $number = 1;
  private $file;
  private $size = "300";
  private $cap_width = "";
  private $caption  = "";
  private $caption_style  = "";
  private $style = "";
  private $align_style = "";
  private $data_title = "";
  private $data_lightbox = "";
  private $alt = "not_set";
  private $extension = "";

  public function set_number($num){
    $this -> number = $num;
  }

  public function set_size($size){
    $this -> size = $size;
  }

  public function set_caption_width($width){
    $this -> cap_width = $width;
  }

  public function set_file($fn){
    $this -> file = $fn;
    $this -> extension = pathinfo($fn, PATHINFO_EXTENSION);
  }
  
  public function set_caption($cap){
    $this -> caption = $cap;
  }

  public function set_caption_style($cap_style){
    $this -> caption_style = $cap_style;
  }

  public function set_style($style){
    $this -> style = $style;
  }

  public function set_align_style($align_style){
    $this -> align_style = $align_style;
  }

  public function set_data_title($title){
    $this -> data_title = $title;
  }

  public function set_data_lightbox($dat){
    $this -> data_lightbox = $dat;
  }

  public function set_alt($alt){
    $this -> alt = $alt;
  }

  public function show_fig_with_LightBox(){
    if (strcasecmp($this->alt, "not_set") == 0) {
      $this->alt = $this->caption;
    }

    if (strcasecmp($this->extension, "svg") == 0) {
      $this -> style = "background-color:white; ".$this -> style;
    }

    printf("<a href=\"%s\" data-lightbox=\"%s\" data-title=\"%s\">\n", $this->file, $this->data_lightbox, $this->data_title);
    printf("<img src=\"%s\" width=\"%s\" style=\"%s\" alt=\"%s\"></a>\n", $this->file, $this->size, $this -> style, $this->alt);
  }

  public function show_fig(){
    printf("<img src=\"%s\" width=\"%s\">\n", $this->file, $this->size);
  }

  public function show_caption(){
    printf("図%d: %s\n", $this->number, $this->caption);
  }

  public function fig_txt($pre, $num){
    if($pre==NULL){
      if($num) printf("%d", $this->number);
    }else{
      if($num){
    printf("%s%d", $pre, $this->number);
      }else{
    printf("%s", $pre);
      }
    }
  }

  public function ref($pre=NULL, $Lightbox=True, $num=True){    
    if($Lightbox){
      printf("<a href=\"%s\" data-lightbox=\"%s\" data-title=\"%s\">", $this->file, $this->data_lightbox, $this->data_title);
      $this -> fig_txt($pre, $num);
      printf("</a>");
    }else{
      $this -> fig_txt($pre, $num);
    }
  }



  // ----- 図とキャプションを出力する関数 -----
  public function show_figure($shadow=False, $center=True, $marginleft=0){
    if (strcasecmp($this->alt, "not_set") == 0) {
      $this->alt = $this->caption;
    }
    if (strcasecmp($this->cap_width, "") == 0) {
      $this->cap_width = $this->size;
    }
    if (strcasecmp($this->extension, "svg") == 0) {
      $this -> style = "background-color:white; ".$this -> style;
    }
    if ($center) {
      printf("<div class=\"flex_align_fig_cap center\" style=\"%s\">\n", $this->align_style);
    }else{
      printf("<div class=\"flex_align_fig_cap\" style=\"margin-left:%dpx; %s\">\n", $marginleft, $this->align_style);
    }
    printf("\t<div>\n");
    printf("<a href=\"%s\" data-lightbox=\"%s\" data-title=\"%s\">\n", $this->file, $this->data_lightbox, $this->data_title);
    if ($shadow) {
      printf("<img src=\"%s\" width=\"%s\" style=\"box-shadow:8px 8px 10px 0px rgba(0,0,0,0.7); %s\" alt=\"%s\"></a>\n", $this->file, $this->size, $this -> style, $this->alt);
    }else{
      printf("<img src=\"%s\" width=\"%s\" style=\"%s\" alt=\"%s\"></a>\n", $this->file, $this->size, $this -> style, $this->alt);
    }
    if (strcasecmp($this->caption, "") != 0) { 
      printf("\t</div>\n");
      printf("\t<div style=\"width:%dpx\">\n", $this->cap_width);
      printf("<p style=\"margin-left:0px; padding-left:0px; %s\">図%d: %s</p>\n", $this->caption_style, $this->number, $this->caption);
      printf("\t</div>\n");
      printf("\t</div>\n");
    }else{
      printf("\t</div>\n");
      printf("\t</div>\n");
    }
  }

}


//============================================================
//    class LightBox
//       class for using LightBox easily
//============================================================
class LightBox{
  private $file;
  private $tag = "";
  private $caption = "";  
  private $menber = "";
  private $extension = "";

  public function set_file($fn){
    $this -> file = $fn;
    $this -> extension = pathinfo($fn, PATHINFO_EXTENSION);
  }
  
  public function set_tag($tag){
    $this -> title = $tag;
  }

  public function set_caption($caption){
    $this -> caption = $caption;
  }

  public function set_member($member){
    $this -> member = $member;
  }

  public function show_lightbox(){
    printf("<a href=\"%s\" data-lightbox=\"%s\" data-title=\"%s\">", $this->file, $this->member, $this->caption);
    printf("%s", $this->title);
    printf("</a>");

  }

}


//============================================================
//   source2html($file, $comment, $line_num, $digit, $pre, $ntab)
//              $file:     file name
//              $comment:  comment mark(text)
//              $line_num: line number 0: off 1: on
//              $digit:    number of digit
//              $pre:      <pre>output 0:off 1:on
//              $ntab:     convert tab to n space(&nbsp)
//                         if < 0 do not convert tab
//              $red_line   color:red lines array
//============================================================
function source2html($file, $comment='', $line_num=1, $digit=3, $pre=0, $ntab=4, $red_line){

  //---------- check char encoding -----
  $cmd = sprintf("nkf -g %s", $file);
  $enc = shell_exec($cmd);
  $enc = trim($enc);
  if ((strcasecmp($enc, "euc-jp") == 0) or (strcasecmp($enc, "ascii") == 0)){
    $enc_sw = FALSE;
  }else{
    $enc_sw = TRUE;
  }
  
  //---------- ready to precessing ----------------------
  $source = fopen($file,"r");
  $ln = 0;

  if($comment == ""){
    $comment_process = 0;
  }else{
    $comment_process = 1;
  }

  //--------- make html strings ----------------------

  //--- for processing commet charactor (1/3)  ----------------------------------
  //--- the following lines for pressing "//" type comments. ------------------- 
  if(preg_match('/.*\/.*/', $comment)){            // ex: if $comment include '/'
    $cmt_tmp = str_replace('/', '\/',$comment);    // ex: convert '/' to "\/" in $comment
    $cmt_match = sprintf("/.*%s.*/", $cmt_tmp);    // ex: $cmt_match   = "/.*\/\/*./"
  }else{
    $cmt_match = sprintf("/.*%s.*/", $comment);    // ex: $cmt_match   = "/.*#.*/"
  }

  if($pre){
    print '<pre>'."\n";
  }

  while(!feof($source)){
    $ln++;
    $line = fgets($source, 1024);
    if($line === false){break;}

    //--- for processing commet charactor (2/3)  ----------------------------------
    //--- shoud be devided three process (1/3), (2/3) and (3/3). 
    //--- because for assembly ';' is comment which includes special chacter in html ---  
    if($comment_process and preg_match($cmt_match, $line)){
      if(!($ln == 1 and preg_match("/^#!/",$line))){      // ignnore shebang line
    //--- single quote and double quote processing ---------
    $quote_test = strstr($line, $comment, true);
    $sq = substr_count($quote_test, "'");
    $dq = substr_count($quote_test, '"');
    if($sq%2 == 0 and $dq%2 == 0){ 
      $line = str_replace($comment, 'x07', $line);      // convert comment char to BEL(0x07)
    }
      }
    }

    //--- for processing html special charactors --- //
    $line = str_replace("&", "&amp;", $line);
    $line = str_replace("<", "&lt;", $line);
    $line = str_replace(">", "&gt;", $line);

    //--- for processing tab charactor --- //
    while(0 < $ntab and  preg_match('/\t/', $line)){
      $nws = $ntab - strpos($line, "\t") % $ntab;               //needed white space
      $tab = white_space($nws);
      $line = preg_replace('/\t/', $tab, $line, 1, $count);     // replace /t to white space
    }
    
    //--- for processing commet charactor (3/3) --- //
    if($comment_process and preg_match('/x07/', $line)){
      $line = rtrim($line);                          // delete return char at end of line
      $line = preg_replace("/x07/", "<span style=\"color:green\">".$comment, $line, 1, $count);
      $line = $line."</span>\n";
    }

    //--- for processin red line --- //
    if (in_array($ln, $red_line)){
      $line = "<span style=\"color:red\">".$line."</span>";
    }


    //--- add line number --- //
    if($line_num){
      $fmt  = sprintf("<span class=\"line_num\">%%0%dd&nbsp;&nbsp;&nbsp;</span>%%s", $digit);
      $line = sprintf($fmt, $ln, $line);
    }

    if($enc_sw){
      $line = mb_convert_encoding($line, "EUC-JP", $enc);
    }
    print "$line";
  }

  if($pre){
    print '</pre>'."\n";
  }
  fclose($source);
}


//============================================================
//   source2html($file, $ntab)
//              $file:     file name
//              $ntab:     convert tab to n space(&nbsp)
//                         if < 0 do not convert tab
//============================================================
function source2html_raw($file, $ntab=4){
  //---------- check char encoding -----
  $cmd = sprintf("nkf -g %s", $file);
  $enc = shell_exec($cmd);
  $enc = trim($enc);
  if ((strcasecmp($enc, "euc-jp") == 0) or (strcasecmp($enc, "ascii") == 0)){
    $enc_sw = FALSE;
  }else{
    $enc_sw = TRUE;
  }

  //---------- ready to precessing ----------------------
  $source = fopen($file,"r");
  $ln = 0;

  while(!feof($source)){
    $line = fgets($source, 1024);
    if($line === false){break;}

    //--- for processing html special charactors --- //
    $line = str_replace("&", "&amp;", $line);
    $line = str_replace("<", "&lt;", $line);
    $line = str_replace(">", "&gt;", $line);

    //--- for processing tab charactor --- //
    while(0 < $ntab and preg_match('/\t/', $line)){
      $nws = $ntab - strpos($line, "\t") % $ntab;               //needed white space
      $tab = white_space($nws);
      $line = preg_replace('/\t/', $tab, $line, 1, $count);     // replace /t to white space
    }

    if($enc_sw){
      $line = mb_convert_encoding($line, "EUC-JP", $enc);
    }
    print "$line";
  }

  fclose($source);
}


//============================================================
//   white_space($num=1)
//              $num: number of white space output
//============================================================
function white_space($num=1){

  $ws = "";
  for($i=0; $i<$num; $i++){
    $ws = $ws." ";            //do not use spcial charctor
  }

  return $ws;
}


//============================================================
//   make lecture top with google adsense
//
//============================================================
function mk_lecture_top($is_google_adsense=TRUE, $ga_data="lecture_top.dat"){

  if($is_google_adsense){
    $google_adsense_path = "http://localhost/yamamoto/data/google_adsense/";
    $in_file = $google_adsense_path.$ga_data;

    $in = fopen($in_file, "r");
    while(!feof($in)){
      $line = fgets($in, 1024);
      print "$line";
    }
    fclose($in);
  }
}


?>