1

I have plain txt file like:

===
Date:30.05.2013
**Header**
text

===
Date:29.05.2013
**Header**
text

===
etc.

I want to convert it into html file like:

<hr>
<b>Date:30.05.2013</b>
<h1>Header</h1>
text
<br>
<hr>
<b>Date:29.05.2013</b>
<h1>Header</h1>
text
<br>
<hr>
etc.

I know about "for" command, i use it

for /f "tokens=*" %%f in ('type news.txt') do (
if [%%f]==[===] (echo ^<hr/^> >>news.htm) ELSE (echo %%f^<br/^> >>news.htm)
)

But i dont know, how perform other action for strings that contains keyword (e.g. Date or *) and dont know, how insert blank br tag for blank strings in text file.

Please, help me, i spent many hours =(

pokatusher
  • 13
  • 1
  • 3

1 Answers1

2
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET "br=^<br^>"
SET "hr=^<hr^>"
SET "h1=^<h1^>"
SET "sh1=^</h1^>"
SET "bold=^<b^>"
SET "sbold=^</b^>"

(
FOR /f "delims=" %%i IN ('type news.txt^|findstr /n "$"') DO (
SET line=%%i&CALL :process
)
)>news.html

GOTO :eof

:process
:: remove line number from line
SET "line=%line:*:=%"
IF NOT DEFINED line ECHO(%br%&GOTO :EOF
SET "line2=%line:"=_%"
SET "line3=%line:"=%"
IF NOT "%line2%"=="%line3%" GOTO rawout
IF "%line%"=="==="  ECHO(%hr%&GOTO :EOF
IF "%line:~0,5%"=="Date:"  ECHO(%bold%%line%%sbold%&GOTO :EOF
IF "%line:~0,2%%line:~-2%"=="****" ECHO(%h1%%line:~2,-2%%sh1%&GOTO :EOF
:rawout
ECHO(!line!%br%
GOTO :eof

This should work for you. It numbers each line, then assigned the numbered line to line. This is a common technique since for /f will skip empty lines.

:process merely looks for keystrings and outputs an appropriate replacement.

I've used a shortcut on detecting 'begins and ends "**". There are more reliable ways of doing it - but it should only fail if the line is***or**` - relatively easy to fix if it's a problem...

(edit 20130531-0134Z new :process procedure to changed specification)

{re-edit 20130531-0750Z add enabledelayedexpansion to setlocal and echo !line! after :rawout to suit unbalanced-quotes}

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • It great! One moment - last line must be ECHO(%line%%br% – pokatusher May 30 '13 at 18:03
  • One bug, that if string contain quote " script stop and no more parse. How fix it? – pokatusher May 30 '13 at 18:07
  • magnificently! one much more small bug - if i use only one quote in string, it visible, but with excess ^ symbol. – pokatusher May 31 '13 at 07:37
  • And one more wish - parse links to net disk folders and files. E.g. "Q:\Project\Blabla\File.txt" must be converted in a link on this file. I think, possible find construction like "Q:\" or "\\server\" – pokatusher May 31 '13 at 07:40
  • Unbalanced-quotes fixed. Not sure what you mean by your link-conversion. Qhat pattern needs to be found, and replaced by what (explicitly) – Magoo May 31 '13 at 07:54
  • Pattern "Q:\anytexthere". i.e. search text in quotes by first three (or more)symbols after open quote – pokatusher May 31 '13 at 08:43
  • I use this construction for autocreate marked lists: `IF "%line:~0,1%"=="-" ECHO(%li%%line:~1,100%%sli%&GOTO :EOF` it work, but if string contain quote - it not work. Why is that? – pokatusher May 31 '13 at 09:06
  • `"%line:~0,1%"=="-"` is evaluated as `"contains"quote"=="-"` The parser believes that the first string is `"contains"` and doesn't understand `quote` since it's expecting `==`. Note how I've set `line2` and `line3`. This replaces the `"` in `line` with `_` or `:`. The point is that `line2`==`line3` ONLY if the two lines DO NOT contain `"`. If you were to try `IF "%line2:~0,1%"=="-" ECHO(%li%!line:~1!%sli%&GOTO :eof` - that will work (note the use of !line:~1! - this postpones the evaluation until AFTER the parser has finished. Also, a missing "length" gives "the rest of the string". – Magoo May 31 '13 at 13:49