LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-02-2017, 05:18 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
TXT2HTML in C language?


Hello,

It is really simple to convert txt to html, since basically it needs to be added <br> at the end of line and also to
make sure that the special chars of html such as '<', '>', tab,... will be converted to be seen with the HTML document.
https://www.w3schools.com/html/html_entities.asp

There is txt2html but it is still a large project, and it does not work always.

Would you know an efficent txt2html which would do the trick relatively simply?

Thank you
 
Old 08-03-2017, 03:58 AM   #2
MrElusive603
LQ Newbie
 
Registered: Jul 2017
Location: United Kingdom
Distribution: Slackware 14.2
Posts: 23

Rep: Reputation: 23
Hi Xeratul,

I've never seen a text-to-HTML converter that keeps tabs. Special characters like > and < should work in text2html. However, I did spot this on the txt2html website:

Quote:
txt2html is not a program to convert wordprocessor files or other marked-up document formats.
This page about HTML indenting is an interesting read. The author summarizes that the best way to get tabs/indenting is using CSS. I presume using CSS is beyond the scope of what you're trying to achieve?

Then again I'm not a web developer lol. I'll have another look a bit later. If I find a converter that parses tabs and special characters I'll let you know.
 
Old 08-03-2017, 04:02 AM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by MrElusive603 View Post
Hi Xeratul,

I've never seen a text-to-HTML converter that keeps tabs. Special characters like > and < should work in text2html. However, I did spot this on the txt2html website:



This page about HTML indenting is an interesting read. The author summarizes that the best way to get tabs/indenting is using CSS. I presume using CSS is beyond the scope of what you're trying to achieve?

Then again I'm not a web developer lol. I'll have another look a bit later. If I find a converter that parses tabs and special characters I'll let you know.
Thank you !!

Perl is not possible, I would be glad to find it in C since it must have been done already in C.
HTML is since some time about 1985 so that must exist, I am sure.

I look for a better efficient alternative to the one here: https://packages.debian.org/search?keywords=txt2html
 
Old 08-03-2017, 05:03 AM   #4
MrElusive603
LQ Newbie
 
Registered: Jul 2017
Location: United Kingdom
Distribution: Slackware 14.2
Posts: 23

Rep: Reputation: 23
Quote:
Originally Posted by Xeratul View Post
Thank you !!
You're welcome.

Have you seen this thread? jschiwal proposed a solution using sed. Granted it's an ancient thread over a decade old, but it might be worth having a look.

Also if you want an indent/tab, apparently you can use &emsp; to achieve this. For example:

Code:
&emsp;Check out my awesome indented line of text!<br />
I wonder if it's possible to automate replacing tabs with &emsp; instead? Maybe a bash script or something?

The only other alternatives I can find are online-based text-to-HTML converters...but your mileage may vary.
 
Old 08-03-2017, 05:50 AM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by MrElusive603 View Post
You're welcome.

Have you seen this thread? jschiwal proposed a solution using sed. Granted it's an ancient thread over a decade old, but it might be worth having a look.

Also if you want an indent/tab, apparently you can use &emsp; to achieve this. For example:

Code:
&emsp;Check out my awesome indented line of text!<br />
I wonder if it's possible to automate replacing tabs with &emsp; instead? Maybe a bash script or something?

The only other alternatives I can find are online-based text-to-HTML converters...but your mileage may vary.

1)) the HTML looks not easy to handle, man, so complicateed with all thsoe &&& and convertions


2)) I must believe that with simple fd and adaption it is much much efficient and better than an heavy bloat using Perl or bash pipes

Code:

///// some cat!! 
#include <fcntl.h>
#include <unistd.h>
static int cat_fd(int fd) 
{
  char buf[4096];
  ssize_t nread;

  while ((nread = read(fd, buf, sizeof buf)) > 0) 
  {
    ssize_t ntotalwritten = 0;
    while (ntotalwritten < nread) {
      ssize_t nwritten = write(STDOUT_FILENO, buf + ntotalwritten, nread - ntotalwritten);
      if (nwritten < 1)
        return -1;
      ntotalwritten += nwritten;
    }
  }

  return nread == 0 ? 0 : -1;
}

static int ncat_static(const char *fname) {
  int fd, success;
  if ((fd = open(fname, O_RDONLY)) == -1)
    return -1;

  success = cat_fd(fd);

  if (close(fd) != 0)
    return -1;

  return success;
}

Last edited by Xeratul; 08-03-2017 at 05:51 AM.
 
Old 08-03-2017, 05:58 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
simple fd will not influence the complexity of the text/html. Also perl is very good at text processing, and also it has a lot of libraries (already available) to handle html pages. Do not need to reinvent the wheel.
I have no idea how bash pipes are related to it?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A problem. When using assembly language to call the C language function mirage1993 Programming 3 10-03-2014 08:15 AM
Help needed to install txt2html pinga123 Linux - Newbie 1 04-15-2011 02:14 AM
[SOLVED] Can the language of fedora boot message change to other language, zh_CN,e.g.? jimtony Fedora 2 01-11-2011 03:03 AM
Good linux chinese language language program? darsunt Linux - Software 1 04-10-2009 12:06 PM
cant compile txt2html hasher Linux - Software 1 04-01-2005 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration