I’m writing a PHP framework right now, and have run into somewhat of a formatting problem.
My intention is to load in a combination of a few files, compile them all together, and kick them out as their appropriate HTML. In this process, I intend to indent or outdent as I progress line to line. Right now, I use a combination of simple checks for ‘<‘, ‘</’ and ‘/>’, ( Couldn’t use the real character here ) alongside some XML parsing to achieve this, but I’m sure that there is a better way to do this.
Right now my code gives me something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Sitename - Module - Index</title>
<link rel="stylesheet" href="layout/layout/default/css/styles.css" type="text/css" />
<link rel="stylesheet" href="php/plugins/MoviePlayer/windows/IE/Silverlight/css/silverlight.css" type="text/css" />
<script type="text/javascript" src="js/Cookies.js"></script>
<script type="text/javascript" src="js/GlobalReady.js"></script>
<script type="text/javascript" src="js/PluginDetection.js"></script>
<script type="text/javascript" src="js/Silverlight.js"></script>
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="js/jquery.flow.1.2.js"></script>
<script type="text/javascript" src="layout/layout/default/js/LayoutInit.js"></script>
<script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/SLPlayer.js"></script>
<script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/wmvplayer.js"></script>
</head>
<body onLoad="javascript: GlobalReady();">
<div id="container">
<div id="header">
</div>
</div>
</body>
</html>
Which is a little off of what I am trying to achieve.
I’m not any sort of regular expression expert; or user even, and I assume that I could achieve this in that manner, but wouldn’t know how to go about it.
Effectively, my question is this; how would You approach formatting this properly ( programatically ), if you were printing it out one line at a time? Is there a library that could identify all of the elements on a single line, regardless of their association with a later element?
If not I’m sure I’ll be writing a new class to handle this on my own.
I am trying to achieve something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Your Sitename - Module - Index</title>
<link rel="stylesheet" href="layout/layout/default/css/styles.css" type="text/css" />
<link rel="stylesheet" href="php/plugins/MoviePlayer/windows/IE/Silverlight/css/silverlight.css" type="text/css" />
<script type="text/javascript" src="js/Cookies.js"></script>
<script type="text/javascript" src="js/GlobalReady.js"></script>
<script type="text/javascript" src="js/PluginDetection.js"></script>
<script type="text/javascript" src="js/Silverlight.js"></script>
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="js/jquery.flow.1.2.js"></script>
<script type="text/javascript" src="layout/layout/default/js/LayoutInit.js"></script>
<script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/SLPlayer.js"></script>
<script type="text/javascript" src="php/plugins/MoviePlayer/windows/IE/Silverlight/js/wmvplayer.js"></script>
</head>
<body>
</body>
</html>
Thanks ahead of time.
Load a few files, concatenate them into a string, and use
tidyto format them.However, it’s most likely a problem with the code generating the HTML, that should be fixed instead of outputting malformed code and fixing it later.