Skip to content

Instantly share code, notes, and snippets.

@tenman
Created February 19, 2010 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenman/308298 to your computer and use it in GitHub Desktop.
Save tenman/308298 to your computer and use it in GitHub Desktop.
<?php
/*
このクラスは、プログラマが作成時に指定した文字列を、htmlコーディングをする時に、コーディングするときに
文字列や、そのブロックのデザインをプログラムに触らずに、変更できるようにしたくて、作成したしょぼいクラスです。
大きなブロックなどは、smarty等を使ったりして描けばいいと思います。このクラスは、assignする値のところなどで使います。
コードをブラウザで閲覧しながら、デザイナーの人が、ちょっと気に入らない文字列をこぴぺして、配列に追加してくれれば、プログラムに触らなくても、文字列や、そこんとこのhtmlブロックの、文字列の直後に、ちっちゃな画像も追加しておきたい。といった、表現する人の主張を、「どうぞ、どうぞ」します。
使い方
* コーディング 翻訳 レイアウトファイル
* ht_coding.php 用の 変換テーブル
*
* tag 文字列は、html elementに書き換え 
* groupは id class 属性 valueは テキスト に置換されます
* 元のvalueに対応した テキストの変更は、配列[1]を書き足して変更できます
* 例:"プログラム上の文字列" => array("<tag group>value</tag>","自分の好きな文字列"),
* array(クラス名または、テキストそのものの値 => array( htmlタグ配置,[翻訳文] );
$coding = array(
"was entryed" => array("<taggroup>value</tag>","さんはエントリーしました"),
"default" => array("<taggroup>value</tag>"),
"someone used this mail" => array("<taggroup>value</tag>","メールアドレスは、登録済みです"),
"we need name" => array("<taggroup>value</tag>","名前は必ず入力してください"),
);
$ht = new ht_coding($coding);
echo $ht->tag("was entryed","h3.test")."\n";
echo $ht->tag("someone used this mail","h3")."\n";
echo $ht->tag("we",".test")."\n";
echo $ht->tag("need","span")."\n";
echo $ht->tag("we need name","#test")."\n";
echo $ht->tag(
"\n\t".$ht->tag("タイトルの記述","h3")."\n\t".
$ht->tag("概要の記述","p.description")."\n\t".
$ht->tag("内容の記述","p")."\n"
,"div.example")."\n";
ht_coding to code html happily, it made it.
Copyright (C) 2010 tenman
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class ht_coding{
public $html4 = '(?i:a|abbr|acronym|address|applet|area|b|base|basefont|bdo|big|blockquote|body|br|button|caption|center|cite|code|col|colgroup|dd|del|dfn|dir|div|dl|dt|em|fieldset|font|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|hr|html|i|iframe|img|input|ins|isindex|kbd|label|legend|li|link|map|menu|meta|noframes|noscript|object|ol|optgroup|option|p|param|pre|q|s|samp|script|select|small|span|strike|strong|style|sub|sup|table|tbody|td|textarea|tfoot|th|thead|title|tr|tt|u|ul|var)';
public $html5 = '(?i:html|head|title|base|link|meta|style|script|noscript|body|section|nav|article|aside|h1|h2|h3|h4|h5|h6|hgroup|header|footer|address|p|hr|br|pre|blockquote|ol|ul|li|dl|dt|dd|a|q|cite|em|strong|small|mark|dfn|abbr|time|progress|meter|code|var|samp|kbd|sub|sup|span|i|b|bdo|ruby|rt|rp|ins|del|figure|figcaption|img|iframe|embed|object|param|video|audio|source|canvas|map|area|table|caption|colgroup|col|tbody|thead|tfoot|tr|td|th|form|fieldset|label|input|button|select|datalist|optgroup|option|textarea|keygen|output|details|summary|command|menu|legend|div)';
public $coding;
/**
* コンストラクタ
*
*
*
*/
function __construct($coding = array("default" => array("<taggroup>value</tag>")
)){
$this->coding = $coding;
}
/**
* デストラクタ
*
*
*
*/
function __destruct(){
}
public function parseElement($class =""){
/**
* 関数からのエレメントの指示を読み出す
*
*
*
*/
if(preg_match("/(.*)?(#|\.)(.+)$/",$class,$regs)){
/**
* htmlエレメントが指定されているか
*
*
*
*/
if(!empty($regs[1])){
$element = $regs[1];
}else{
$element = "p";
}
/**
* クラス名 IDの指定、テンプレート指定をチェック
* クラス名がマルチクラス指定である場合、最初のクラス指定を
* 適用テンプレートの名称と照合して、適用可能であれば、適用する
* IDは複数指定できない
*/
if(!empty($regs[3])){
if(preg_match("/([^\s]*)\s/",$regs[3],$regs2)){
$key_attr = $regs2[1];
}else{
$key_attr = $regs[3];
}
}else{
$key_attr = "";
}
/**
* クラスなのか IDなのか
*
*
*
*/
if(!empty($regs[2])){
switch($regs[2]){
case("#"):
//マルチidは許容しない
$attr = ' id="'.$key_attr.'"';
break;
case('.'):
//マルチクラスは許容する
$attr = ' class="'.$regs[3].'"';
break;
}
}else{
$attr = "";
}
}else{
/**
* パターンで照合しきれない値は、htmlエレメントまたは、
* テンプレートクラス指定とみなす。
* 後で、htmlエレメントかどうか照合し、そうであれば、採用
* 一致しない場合は、不採用となる。
*
*/
$element = $class;
$attr = "";
$key_attr = "default";
}
/**
* クラスパターンに合致するテンプレートがないときには、標準のパターンを使う
*
*
*
*/
if(!array_key_exists($key_attr, $this->coding)){
$key_attr = "default";
}
$result = array();
/**
* みなしエレメントがhtmlエレメントかどうか検査する。
*
*
*
*/
if(isset($element) and preg_match("/$this->html4/",$element)){
$result['element'] = $element;
}else{
$result['element'] = "p";
}
/**
*
*
*
*
*/
if(isset($attr)){
$result['attr'] = $attr;
}else{
$result['attr'] = "";
}
/**
*
*
*
*
*/
if(isset($key_attr)){
$result['key_attr'] = $key_attr;
}else{
$result['key_attr'] = "default";
}
return $result;
}
public function tag($str,$class=""){
$str = $this->parse_strings($str);
extract($this->parseElement($class), EXTR_PREFIX_SAME, "attr");
//if(empty($this->coding[$key_attr][1])){
if( array_key_exists($str,$this->coding)
and
!empty($this->coding[$str][1])
){
/* return sprintf(
$this->coding[$str][0],
$element,
$attr,$this->coding[$str][1],
$element
);*/
$b = array("tag","group","value");
$a = array($element,$attr,$this->coding[$str][1]);
$a = str_replace($b,$a,$this->coding[$str][0]);
return $a;
}
/*return sprintf(
$this->coding[$key_attr][0],
$element,
$attr,
$str,
$element
);*/
$b = array("tag","group","value");
$a = array($element,$attr,$str);
$a = str_replace($b,$a,$this->coding[$key_attr][0]);
return $a;
}
public function parse_strings($str,$element_list = "html4"){
if(is_object($str)){
$this->tag($str);
}
return $str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment