ultimix
auto_markup.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
27 
38  var $Cache = false;
39  var $CachedMultyFS = false;
40  var $ConfigSettings = false;
41  var $Settings = false;
42  var $String = false;
43  var $Utilities = false;
44 
55  var $MacroScripts = false;
56 
67  var $ParsedConfig = false;
68 
83  function __construct()
84  {
85  try
86  {
87  $this->Cache = get_package( 'cache' , 'last' , __FILE__ );
88  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
89  $this->ConfigSettings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
90  $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
91  $this->TemplateContentAccess = get_package(
92  'page::template_content::template_content_access' , 'last' , __FILE__
93  );
94  $this->String = get_package( 'string' , 'last' , __FILE__ );
95  $this->Utilities = get_package( 'utilities' , 'last' , __FILE__ );
96  $this->ParsedConfig = array();
97  }
98  catch( Exception $e )
99  {
100  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
101  }
102  }
103 
126  private function get_name( &$Config )
127  {
128  try
129  {
130  if( ( $Name = $Config->get_setting( 'macro_name' , false ) ) !== false )
131  {
132  return( $Name );
133  }
134  elseif( ( $Name = $Config->get_setting( 'block_name' , false ) ) !== false )
135  {
136  return( $Name );
137  }
138  throw( new Exception( 'Macro name was not found' ) );
139  }
140  catch( Exception $e )
141  {
142  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
143  }
144  }
145 
160  private function load_template_contents_configs_from_file()
161  {
162  try
163  {
164  $this->MacroScripts = $this->CachedMultyFS->get_config( __FILE__ , 'cf_macro_scripts' );
165  $this->MacroScripts = explode( "\n" , $this->MacroScripts );
166 
167  foreach( $this->MacroScripts as $k => $v )
168  {
169  $this->ConfigSettings->load_settings( $v );
170 
171  $Name = $this->get_name( $this->ConfigSettings );
172 
173  $this->ParsedConfig[ $Name ] = $this->ConfigSettings->get_raw_settings();
174  }
175 
176  $this->Cache->add_data( 'parsed_macro_config' , serialize( $this->ParsedConfig ) );
177  }
178  catch( Exception $e )
179  {
180  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
181  }
182  }
183 
198  private function load_template_contents_configs()
199  {
200  try
201  {
202  if( count( $this->ParsedConfig ) == 0 )
203  {
204  if( $this->Cache->data_exists( 'parsed_macro_config' ) )
205  {
206  $this->ParsedConfig = unserialize( $this->Cache->get_data( 'parsed_macro_config' ) );
207  }
208  else
209  {
210  $this->load_template_contents_configs_from_file();
211  }
212  }
213  }
214  catch( Exception $e )
215  {
216  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
217  }
218  }
219 
246  private function compile_macro( &$Config , &$Settings )
247  {
248  try
249  {
250  if( $Config->get_setting( 'template' , false ) )
251  {
252  $Content = $this->TemplateContentAccess->get_content_ex( $Config );
253  }
254  else
255  {
256  $Package = $this->Utilities->get_package( $Config , __FILE__ );
257  $Function = $Config->get_setting( 'compilation_func' , false );
258  $Content = call_user_func( array( $Package , $Function ) , $Settings );
259  }
260 
261  if( $Settings !== false )
262  {
263  $Content = $this->String->print_record( $Content , $Settings->get_raw_settings() );
264  }
265 
266  return( $Content );
267  }
268  catch( Exception $e )
269  {
270  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
271  }
272  }
273 
304  private function compile_block( &$Config , &$Settings , $Data )
305  {
306  try
307  {
308  if( $Config->get_setting( 'template' , false ) )
309  {
310  $Content = $this->TemplateContentAccess->get_content_ex( $Config );
311  }
312  else
313  {
314  $Package = $this->Utilities->get_package( $Config , __FILE__ );
315  $Function = $Config->get_setting( 'compilation_func' , false );
316  $Content = call_user_func( array( $Package , $Function ) , $Settings , $Data );
317  }
318 
319  if( $Settings !== false )
320  {
321  $Content = $this->String->print_record( $Content , $Settings->get_raw_settings() );
322  }
323 
324  return( $Content );
325  }
326  catch( Exception $e )
327  {
328  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
329  }
330  }
331 
362  private function compile_simple_macro( &$Config , $Str , $Changed )
363  {
364  try
365  {
366  $Name = $this->get_name( $Config );
367 
368  if( strpos( $Str , '{'.$Name.'}' ) !== false )
369  {
370  $this->Settings->clear();
371  $this->set_default_values( $this->Settings , $Config );
372  $Content = $this->compile_macro( $Config , $this->Settings );
373  $Str = str_replace( '{'.$Name.'}' , $Content , $Str );
374  $Changed = true;
375  }
376 
377  return( array( $Str , $Changed ) );
378  }
379  catch( Exception $e )
380  {
381  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
382  }
383  }
384 
407  private function get_rules( &$Config )
408  {
409  try
410  {
411  $Rules = array();
412 
413  if( $Config->get_setting( 'terminal_values' , false ) !== false )
414  {
415  $TerminalValues = explode( ',' , $Config->get_setting( 'terminal_values' ) );
416 
417  foreach( $TerminalValues as $i => $Name )
418  {
419  $Rules[ $Name ] = TERMINAL_VALUE;
420  }
421  }
422 
423  return( $Rules );
424  }
425  catch( Exception $e )
426  {
427  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
428  }
429  }
430 
453  private function set_default_values( &$Settings , &$Config )
454  {
455  try
456  {
457  if( $Config->get_setting( 'default_values' , false ) !== false )
458  {
459  $DefaultValues = explode( ',' , $Config->get_setting( 'default_values' , false ) );
460 
461  foreach( $DefaultValues as $i => $DefaultValue )
462  {
463  $DefaultValue = explode( ':' , $DefaultValue );
464  $Settings->set_undefined( $DefaultValue[ 0 ] , $DefaultValue[ 1 ] );
465  }
466  }
467  }
468  catch( Exception $e )
469  {
470  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
471  }
472  }
473 
504  private function compile_parametrized_macro( &$Config , $Str , $Changed )
505  {
506  try
507  {
508  $Name = $this->get_name( $Config );
509 
510  $Rules = $this->get_rules( $Config );
511 
512  for( ; $Params = $this->String->get_macro_parameters( $Str , $Name , $Rules ) ; )
513  {
514  $this->Settings->load_settings( $Params );
515  $this->set_default_values( $this->Settings , $Config );
516  $Content = $this->compile_macro( $Config , $this->Settings );
517  $Str = str_replace( '{'."$Name:$Params".'}' , $Content , $Str );
518  $Changed = true;
519  }
520 
521  return( array( $Str , $Changed ) );
522  }
523  catch( Exception $e )
524  {
525  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
526  }
527  }
528 
563  private function compile_single_type_single_block( $Name , $Str , $Params , &$Config )
564  {
565  try
566  {
567  $this->Settings->load_settings( $Params );
568  $this->set_default_values( $this->Settings , $Config );
569 
570  $Data = $this->String->get_block_data( $Str , "$Name:$Params" , "~$Name" );
571 
572  $Content = $this->compile_block( $Config , $this->Settings , $Data );
573 
574  $Str = substr_replace(
575  $Str , $Content.'{'."$Name:$Params".'}' ,
576  strpos( $Str , '{'."$Name:$Params".'}' ) , strlen( '{'."$Name:$Params".'}' )
577  );
578  $Changed = false;
579 
580  return( array( $this->String->hide_block( $Str , "$Name:$Params" , "~$Name" , $Changed ) , true ) );
581  }
582  catch( Exception $e )
583  {
584  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
585  }
586  }
587 
618  private function compile_parametrized_block( &$Config , $Str , $Changed )
619  {
620  try
621  {
622  $Name = $this->get_name( $Config );
623 
624  $Rules = $this->get_rules( $Config );
625 
626  for( ; $Params = $this->String->get_macro_parameters( $Str , $Name , $Rules ) ; )
627  {
628  list( $Str , $Changed ) = $this->compile_single_type_single_block(
629  $Name , $Str , $Params , $Config
630  );
631  }
632 
633  return( array( $Str , $Changed ) );
634  }
635  catch( Exception $e )
636  {
637  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
638  }
639  }
640 
663  private function is_macro( &$Config )
664  {
665  try
666  {
667  if( ( $Name = $Config->get_setting( 'macro_name' , false ) ) !== false )
668  {
669  return( true );
670  }
671  elseif( ( $Name = $Config->get_setting( 'block_name' , false ) ) !== false )
672  {
673  return( false );
674  }
675 
676  throw( new Exception( 'Undefined entity type' ) );
677  }
678  catch( Exception $e )
679  {
680  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
681  }
682  }
683 
714  private function compile_named_macro( $Name , $Str , $Changed )
715  {
716  try
717  {
718  if( isset( $this->ParsedConfig[ $Name ] ) )
719  {
720  $this->ConfigSettings->load_raw_settings( $this->ParsedConfig[ $Name ] );
721  if( $this->is_macro( $this->ConfigSettings ) )
722  {
723  list( $Str , $Changed ) = $this->compile_simple_macro(
724  $this->ConfigSettings , $Str , $Changed
725  );
726 
727  list( $Str , $Changed ) = $this->compile_parametrized_macro(
728  $this->ConfigSettings , $Str , $Changed
729  );
730  }
731  else
732  {
733  list( $Str , $Changed ) = $this->compile_parametrized_block(
734  $this->ConfigSettings , $Str , $Changed
735  );
736  }
737  }
738  return( array( $Str , $Changed ) );
739  }
740  catch( Exception $e )
741  {
742  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
743  }
744  }
745 
776  function compile_all_macroes( &$Options , $Str , $Changed )
777  {
778  try
779  {
780  $this->load_template_contents_configs();
781 
782  $Macroes = $this->String->find_all_macro( $Str );
783 
784  foreach( $Macroes as $i => $Name )
785  {
786  list( $Str , $Changed ) = $this->compile_named_macro( $Name , $Str , $Changed );
787  }
788 
789  return( array( $Str , $Changed ) );
790  }
791  catch( Exception $e )
792  {
793  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
794  }
795  }
796 
819  function compile_string( $Str )
820  {
821  try
822  {
823  $Changed = true;
824 
825  for( ; $Changed ; )
826  {
827  $Changed = false;
828 
829  list( $Str , $Changed ) = $this->compile_all_macroes( $this->Settings , $Str , $Changed );
830  }
831 
832  return( $Str );
833  }
834  catch( Exception $e )
835  {
836  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
837  }
838  }
839  }
840 
841 ?>