Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Make a Markup Method in Perl?

by perl.j (Pilgrim)
on May 16, 2012 at 00:01 UTC ( #970725=perlquestion: print w/replies, xml ) Need Help??

perl.j has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to make a markup method for myself. Something like a breed of HTML and LaTeX but nowhere near as advanced.

I was thinking I could do something like this (in this instance, the tag would make the text bold):

if (text is surrounded by tag) { make text bold } else { leave text alone }

I know that you could check to see if the tag is around the text easily. It's Regular Expressions of course. But does Perl have some kind of module or functionality to change the layout of text? In case it matters, the output would be to another text file, maybe rich text format (.rtf).


Replies are listed 'Best First'.
Re: Make a Markup Method in Perl?
by davido (Cardinal) on May 16, 2012 at 01:43 UTC

    Text markup, whether it's in human readable format such as HTML, or in a machine readable format such as with MS Office, targets a particular rendering engine. HTML targets web browsers (among other entities) that understand HTML. Let's say you manage to come up with a whiz-bang markup language that fits some well-defined niche. What use is it if there is no tool that knows how to render it?

    Once you decide what you want to target (a browser, ...or who-knows-what else), then the markup language is already decided for you.

    Perhaps you want to translate one simplistic markup language of your design into a more complex one that already exists; perl-j-markup2html, perl-j-markup2pod... whatever. What makes your new language better or more practical, and for what application, than what's already out there? Why would I want to learn perl-j-markup when I already know HTML?

    Maybe you already have answers to these questions. If you do, you are well on your way to defining the problem and the use case. But with no use case, you're just in search of a problem that doesn't need a solution.


      Honestly davido, I do have a plan. The only thing I don't know is how to change the look of the text.
        Honestly davido, I do have a plan. The only thing I don't know is how to change the look of the text.
        You have two problems here. You want to invent a markup. Since you don't show any, that's up to you. You have to write a parser for that markup.

        The second problem is you want to change the look of the text. But where is that text to be displayed? if in a terminal, then Term::ANSIColor (which you already mentioned) would be a good choice. but you said above that this module doesn't help you. you only forget to mention why.

        So what do you actually want? if you want help you need to add more information. text is just text and has no format. if you add terminal escape sequences to it, then in a terminal it can be bold, coloured. displaying terminal escape sequences in a browser or in Tk is of no use, on the other hand.

        See Parse::BBCode (one of my modules) for example. It's a simple markup, and the module parses it, returns a parse tree and renders it to HTML or text or any format you define.

      Technically, neither XML nor SGML "targets a particular rendering engine." In fact, they were both designed with device independence as a primary goal.

      Not that I disagree with your point, I'm just picking nits.

Re: Make a Markup Method in Perl?
by chromatic (Archbishop) on May 16, 2012 at 06:20 UTC
    I know that you could check to see if the tag is around the text easily. It's Regular Expressions of course.

    That's the hard way to do it. The easy way to do it is to write a parser to transform text into a data structure, then walk the data structure to transform it into another data structure from which it's easy to emit the right text.

    Improve your skills with Modern Perl: the free book.

      this :)

      OK. But how can I change the layout of the text?

        You're going to have to learn the details of RTF or the API of a module which produces RTF.

        OK. But how can I change the layout of the text?

        Flip your monitor? Seems to work :)

Re: Make a Markup Method in Perl?
by sauoq (Abbot) on May 16, 2012 at 01:29 UTC
    Something like a breed of HTML and LaTeX but nowhere near as advanced.

    Have you heard the phrase "reinventing the wheel?"

    Usually it's something to avoid doing. Why do you want a breed of HTML and LaTeX if you already have HTML and LaTeX? (And any number of other solutions.)

    I'm not saying that there aren't valid answers to this question. There are. But they are few and far between.

    Correct me if I'm wrong, but you seem a little new to this stuff. Why not spend your energy reusing a solution that was created by people with more experience than you, probably only after lots of hard work? There is so much available that it's mind-boggling. And if you can learn from how they did it and maybe even add your own incremental improvements after some time, then all the better.

    "My two cents aren't worth a dime.";
      But I want to reinvent the wheel.


        Ah, so you're doing it as a learning exercise, then? In that case, the first thing I'd do is figure out what the grammar for my markup would be, and what it would mean. Sketch it out on a sheet of paper. Then, start writing code to read the text and parse out your instructions. If it gets nasty, you might change your grammar to simplify it.

        The roff, troff, etc., layout programs kept their grammar simple by using the rule "if a line begins with a period, then it's a layout command, otherwise it's text". So a marked-up file would look like this:

        .TH 7z 1 "September 1 2006" "Mohammed Adnene Trojette" + .SH NAME 7z \- A file archiver with highest compression ratio .SH SYNOPSIS .B 7z .BR [adeltux] .BR [\-] .BR [SWITCH] .BR <ARCHIVE_NAME> .BR <ARGUMENTS>... .PP .SH DESCRIPTION 7-Zip is a file archiver with the highest compression ratio. The progr +am supports 7z (that implements LZMA compression algorithm), ZIP, CAB +, ARJ, GZIP, BZIP2, TAR, CPIO, RPM and DEB formats. Compression ratio + in the new 7z format is 30-50% better than ratio in ZIP format. .TP 7z uses plugins to handle archives. .PP

        This is the first few lines of the man page for 7z. The man system in *NIX uses *roff formatted files. On most(?) Linux systems, you can read man 7 groff to get details on the grammar used.


        When your only tool is a hammer, all problems look like your thumb.

Re: Make a Markup Method in Perl?
by stevieb (Canon) on May 16, 2012 at 00:25 UTC

    You're the person who wanted to become an editor for Perlmonks blog, yes?

    I get an idea of what you're asking, but imho you're getting too far ahead of yourself through your questions. Head on over to CPAN and have a look around.

    Google is also your friend, as is the search utility within this site itself.

    Perl, after all, is the Practical Extraction and Reporting Language.

      I found Term::ANSIColor. I also found a similar Perlmonks Q&A question, but it did not help much. By the way, this has nothing to do with the blog :-)

        I'm just clarifying my point that I feel you may be getting ahead of yourself. What you should do, is produce a problem case containing the issue you want to solve, then supply code with what you've tried to solve it.

        After that, you'll get valuable responses either directly, or that point you to the modules or documentation you need to read.

        Stating that you want to create a new markup language with no real understanding of what the goals are isn't helpful at all. You've been here a year and have a significant amount of posts during that time... I'd think you'd understand that.

        I'm just trying to let you know what I think kindly... things can get harsh quickly here ;)

Re: Make a Markup Method in Perl?
by zentara (Archbishop) on May 16, 2012 at 10:09 UTC
    But does Perl have some kind of module

    Any of the GUI toolkits should be able to do it. Here is a basic Tk example. I don't know if it fullfills all your requirements, but the tricks you can do with tags are much more complex than shown here. Look at the second example below for more techniques including Tk::Text's internal regexp search engine.

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(-title=>'Colored Text'); $mw->geometry('200x200+666+266' ); $mw->fontCreate('big', -family=>'arial', #-weight=>'bold', -size => 12 ); my $text = $mw->Scrolled('Text', -scrollbars=>'osw', -background => 'black', -foreground => 'lightyellow', -font => 'big', )->pack; #add colors and font effects $text->tagConfigure( 'tag1', -foreground => 'red' ); $text->tagConfigure( 'tag2', -foreground => 'skyblue' ); $text->tagConfigure( 'tag3', -foreground => 'yellow'); $text->tagConfigure( 'tag4', -foreground => 'green' ); $text->tagConfigure( 'bold', -font => [ 'Courier New', 20, 'bold' ] +); $text->tagConfigure( 'italic', -font => [ 'Courier New', 20, 'italic' +] ) ; # see regular text $text->insert('end',"some text\n"); # see tagged text $text->insert('end',"some text\n",'tag1'); $text->insert('end',"some text\n",'tag2'); $text->insert('end',"some text\n",['tag3','bold']); $text->insert('end',"some text\n",['tag4','italic']); MainLoop; __END__
    A more advanced example:

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://970725]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-10-01 11:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found