Formatting a simple help file

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Formatting a simple help file

Post by omniuni »

Hi All,

I'm working on a simple help system for a project I'm doing. Rather than parsing the help files as HTML, I want to create a very easy way to pleasantly reformat plain text. So, my idea is as follows.

There will be a folder called "help" in which there will be a bunch of files, each one representing a page in the manual. Each is a plain text file, but may contain the following:

*Headline
#Title
&Pagelink
!Important
@Tip
-Bullet

In other words, if a line starts with such a symbol, some special formatting is applied. For example:

*Headline => <span class="help_headline">Headline</span>

So I was wondering what the easiest/best way to detect one of these symbols at the beginning of a line, pull the text following, and replace it with a formatted version would be. I suspect that because of having to look for the symbol at the beginning of the line, I will have to use regex, but if there is a PHP command similar to str_replace that would do, I would appreciate that as well.

Thanks for your help!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Formatting a simple help file

Post by AbraCadaver »

I would use something that is already in place and has wide adoption such as XML. You can then easily apply a stylesheet or transformation for formatting.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Formatting a simple help file

Post by omniuni »

While there is merit to such things, they're also far more complex to produce than what I'm looking for. If it helps, my design goals for the project:

1. No complex markup.
2. Reads in the file like it does on screen.
3. Easy inclusion in another script

I don't want a file that looks like:

<helpfile for="plugin_name">
<section>
<title>hello</title>
</section>....

When it could just be:

#Hello

Unless there is something I'm missing that would make it less complex than what I think?

I don't want to spend a lot of time writing my help files. I don't want to have to debug them because some piece of it wasn't nested right. I don't want to have to ever escape characters. The goal is type it, add a few symbols to make it look pretty, stick it in the folder with the rest of the files, and it just works.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Formatting a simple help file

Post by omniuni »

This code does mostly what I want:

Code: Select all

<?php

//get the file into a var, make a new one to hold output
$helpfile = file_get_contents('helptest.txt');
$output = null;

//split the file into lines
$delimiters[] = "\r\n";
$delimiters[] = "\n";
//fix strange encodings
$helpfile = str_replace($delimiters, "\r", $helpfile);
$lines = explode("\r", $helpfile);
$lines = array_filter($lines);

//check
print_r($lines);

//for each line, check first char, do switch, and add line to output

foreach($lines as $line){

	$fc = $line[0];
	
	$line = substr($line, 1);

	switch($fc){

		case '*':
			$output.='<p style="font-size: 140%;">'.$line.'</p>';
		break;
		
		case '#':
			$output.='<p style="font-weight: bold;">'.$line.'</p>';
		break;
		
		case '-':
			$output.= '<ul><li>'.$line.'</li></ul>';
		break;
		
		case '!':
			$output.= '<p style="color: red;">'.$line.'</p>';
		break;
		
		case '&':
			$output.= '<p><a href="'.$line.'">'.$line.'</a></p>';
		break;
		
		default:
			$output.=$line;

	}
	
	$output.="\r\n";

}

//output it

echo $output;

?>
Unfortunately, I suspect that it is quite a bit larger and less efficient than it need be, and the condition for the links is a bit odd. I don't always want the link to be on the front of the line. Instead, anywhere in the file where there is a line like this:

[text]&something[/text]

would become:

[text]<a href="something">something</a>[/text]

I know the pattern I need to look for is ampersand-letters_or_numbers-space_or_line_return but I'm not sure how to do that.

Thanks again.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Formatting a simple help file

Post by AbraCadaver »

I'm not sure what you mean by the links question. Give some more detail and I'll take a stab at it. Off the top of my head, here's another approach:

Code: Select all

$search = array('/^\*(.*)$/', '/^#(.*)$/', '/^-(.*)$/', '/^!(.*)$/', '/^&(.*)$/');

$replace = array(	
	'<p style="font-size: 140%;">$1</p>',
	'<p style="font-weight: bold;">$1</p>',
	'<ul><li>$1</li></ul>',
	'<p style="color: red;">$1</p>',
	'<p><a href="$1">$1</a></p>',
);

$lines = file_get_contents('helptest.txt');
$output = '';

foreach($lines as $line) {
	$output .= preg_replace($search, $replace, $line) . PHP_EOL;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Formatting a simple help file

Post by omniuni »

Ah! OK, that's looking more like what I was hoping for!

If I understand right, those are regex that look for lines matching "starts with [whatever]". So to replace some other thing, I just need the regex that matches it.

So.
All I need is to have the condition for & look for it anywhere in the line, not just at the beginning...

Something like... um.... '^&(.*)\ ' ??

And the replace condition would be something like '<a href="$1">$1</a>' if I'm understanding this right.

Thanks for helping me, I am pretty much oblivious when it comes to regex.
Post Reply