Текстовый ассемблер

Текстовый ассемблер

Сергей Шишкин

Текстовый ассемблер, сокращенно TA или TXTASM , представляет собой текстовый?/макрос процессора общего назначения, который берет текстовый файл(ы) в качестве входных данных и собирает их в выходной текстовый файл(ы). Он делает это путем разбора текста и записи его в буфере выходного потока. Этот буфер можно сохранить на диск.

Вместо того, чтобы реализовать собственный язык для любой логики генерации текста, Google с исходным кодом V8 JavaScript Engine был объединен в ассемблере. Это позволяет в полной мере использовать язык ECMAScript®, используя стандарт ECMA-262.

Стандартные макроконтексты JavaScript могут быть востребованы и использованы для выполнения простых задач замены. Для более сложной задачи создания текста можно загрузить модель данных JSON, используемую в расчете с кодом JavaScript для желаемого результата. Ассемблер текста был создан для создания веб-страниц в формате HTML, но также может быть зарегистрирован для создания веб-страниц любого типа текстового файла, например, CSV или даже файлов JavaScript.


Текстовый ассемблер вызывается из командной строки:

ta [опции] -o <каталог> <исходный файл>

По умолчанию имя выходного файла открывается с именем входного файла, поэтому выходной каталог должен быть указан и не должен совпадать с каталогом входного файла, если файл не сохранен с другим именем. Доступны возможные параметры командной строки с учетом регистра.

Имя Описание

-D имя[=значение] … Определить глобальную переменную JavaScript с необязательным значением. Если значение не указано, это означает, что ему присвоено логическое значение «истина». Значения строковых таблиц должны быть согласованы в одинарных кавычках, например

- D url='http://www.akiwi.co.uk'

-I <dir> … Добавить каталог включаемый путь.

-e <кодировка> ... Введите кодировку символов исходного файла, либо «UTF8», либо «WIN1252». По умолчанию используется «UTF8».

-o <каталог> ... Установить выходной каталог. -oc Включить сжатие вывода. Любые прогоны пробелов сокращаются до одного пробела.

-od ... Enable output differencing. If a file of the same name already exists, the file contents are read into a buffer and compared with the contents of the output stream buffer. If they are identical, the save is skipped preserving the existing file and its timestamp.

-w ... Enable warning messages.

-debug ... Enable debug messages.

-help ... Print a summary of all available command line options.

-version ... Show version and copyright information.


Assembler Directives


A small number of assembler directives can be used within a document. All assembler directives begin with a # character. To use a literal # within a document, escape it with ## or turn parsing off then on again.


#off … Disables assembler parsing. Sometimes it's necessary to disable the parser if literal text would be misinterpreted. The #off and any trailing whitespace on the same line are not emitted by the parser. Any leading characters will be emitted. This directive is typ #off Some text not to be parsed by the assembler #on


#on ... Normal assembler parsing is resumed. The #on and any trailing whitespace on the same line are not emitted by the parser. Any leading characters will be emitted.


Assembler Variables


Built-in assembler variables always begin with an underscore '_'. These should be considered as reserved words and not used for any other purpose.


_outdir ... A string containing the output directory that was specified on the command line using the -o option.


JavaScript Functions


Core Functions Here is a table summarising the most useful core functions available in ECMA-262 for the purpose of generating text files. For full documentation, please refer to [Standard ECMA-262. ECMAScript Language Specification. Edition 5.1 (June 2011) http://www.ecma-international.org/publications/standards/Ecma-262.htm ].


Number.toString( [radix] ) ... Returns a string representing this number. Number.toFixed( fractionDigits ) ... Returns a string representing this number with the specfied number of decimal places.

String.indexOf( searchString, position ) ... Finds a substring within a string. String.lastIndexOf( searchString, position ) ... Finds a substring within a string, backwards. String.replace( searchValue, replaceValue ) ... Search and replace within a string. String.search( regexp ) ... Search for a substring using a regular expression.

String.split( separator, limit ) ... Splits and array at each separator and returns an array of substrings excluding the separator.

String.substring( start, end ) ... Extracts a substring from a string.

String.toLowerCase() ... Converts a string to lower case in place.

String.toUpperCase() ... Converts a string to upper case in place.

String.trim() ... Removes leading and trailing whitespace from a string in place.

Date.toUTCString() ... Returns a string representation of this date and time in UTC format. Date.toISOString() ... Returns a string representation of this date and time in ISO format. Date.toDateString() ... Returns a string representation of the date part only.

Date.toTimeString() ... Returns a string representation of the time part only.

JSON.parse( text [, reviver] ) ... Parses a JSON formatted string and produces a JavaScript value.


Extensions These JavaScript functions are available in TA and extend the standard set of JavaScript core functions.


$( s ) ... The inline string function - a special function. Unlike the other functions which must be called within a JavaScript block, this function actually creates its own JavaScript block implicitly and therefore can occur at any location within a text document. It enters a JavaScript block, evaluates the string expression 's' and prints the result to the output stream. e.g. <meta content =="$(tohtml('food ' + '&' + ' drink');)" />


erase( n ) ... Erases the last 'n' characters from the output stream buffer.


lastmod( filename ) ... Returns a JavaScript Date object containing the time at which the specified file was last modified. This function can be used to create XML sitemaps with accurate lastmod elements.


load( filename ) ... Loads a UTF-8 text file into memory, converts it to UTF-16 and then stores the result in a JavaScript string variable. This function can be used to load external strings and data for use during text assembly.

/* Load some JSON data */

var s = load('data.json');

var d = JSON.parse( s );


parse( filename ) ... Include a text file at the current location and parse it. If the file cannot be found, then the -I path list is searched for the named file.


print( s ) ... Write a string to stdout. This can be used to generate status messages during text processing operations.


save( filename [, flags] ) ... Save the output stream buffer to a named file relative to the output directory. After the save, the buffer length is set to zero. This functions behaviour is affected by the command line options -oc and -od which enable output compaction and differencing respectively. If a flags string is specified, then it overrides the global settings. To enable or disable an option, the flag should be preceded by a '+' or '-' character respectively. The following flags are currently available.

c ... Output compaction

d ... Output differencing

Returns true if the file was written out to disk successfully, otherwise false.


tocsv( s [, separator] ) ... Return the string 's' converted to a CSV field, properly double quoted and escaped if necessary. The string is only enclosed in double quotes if it contains a separator, double quote or newline character(s). The integer separator character value is optional and defaults to a comma = 44.

/* Write out a tsv field */

write(tocsv(s,9)+'\t');


tohtml( s ) ... Return a string in HTML format, replacing <, >, &, ', " characters with their corresponding entity references <, >, &, &apos;, ". If an & character starts a recognised HTML entity reference, then no replacement is made. Any characters contained within a CDATA section delimited by <!CDATA [' and ']] > are output verbatim. The CDATA start and end sequences are removed from the string.


topath( s ) ... Return a valid path name, replacing any illegal path name characters within 's' with an underscore. Runs of underscores are reduced to a single occcurance.


toplain( s ) ... Return 's' as plain text without any HTML character entity references.


write( s ) ... Append a string to the output stream buffer.


Text Generation There are various mechanisms available to generate output text. These are discussed below in the relevant section.


Script Blocks Text assembler scripts within a document must be enclosed in a script block with type "application/vnd.akiwi.ta". The inline string function $() is the only function that may be called outside a script block.

<script type="application /vnd.akiwi.ta" >

/* Put your JavaScript here */

</script>


The parse() Function The parse() function can be used to concatenate text files. Each file is loaded and parsed sequentially. If there are no text assembler directives or scripts contained within the file, then the input file is written to the output stream verbatim. For example, if we have 3 plain HTML files, header.html, body.html and footer.html, these can be concatenated and output as a single document using the following script. File: myfile.html

<script type="application /vnd.akiwi.ta" >

parse('header.html');

parse('body.html');

parse('footer.html');

</script>


The write() Function The write() function is used to write a string to the output stream. File: myfile.html

<script type="application /vnd.akiwi.ta" >

write('Hello World\n');

</script>


Conditional Text Generation A standard JavaScript if statement can be used to conditionally generate text. For example, to simulate the C preprocessors #ifdef directive, we can use the JavaScript typeof operator as follows

<script type="application /vnd.akiwi.ta" >

if( typeof myvar === 'undefined' )

write('a');

else

write('b');

</script>


Assuming the string variable myvar exists and can take on several values, then we can also use the following script to conditionally generate text.

<script type="application /vnd.akiwi.ta" >

if( myvar === 'foo' )

write('a');

else if( myvar === 'bar' )

write('b');

else

write('c');

</script>


Iterative Text Generation Standard JavaScript loop statements can be used to iteratively generate text, e.g.

<script type="application /vnd.akiwi.ta" >

var a = new Array("Пн","Вт","Ср","Чт","Пт","Сб","Вс");

for( var n=0; n'+a[n]+'\n');

написать(''+а[n]+'\n');

</скрипт>

https://telegra.ph/PEGAS-10-28

Report Page