Text functions.
Funciones de nombre corto
Text Q(Text txtVal)Text W(Text txtVal)Text R(Text txtLst)Text F(Anything anyVal)Funciones
Set Txt2Set(Text txtInp, Text sepTok)Set TxtTokenizer(Text txtInp, Text tagBrk)Text TxtReplaceFirst(Text txtInp, Text txtOld, Text txtNew)Text TxtBetween2Tag(Text inpTxt, Text tagIni, Text tagEnd, Real cmpFlg)Text TxtInside2TagPlus(Text txtInp, Text tagIni, Text tagEnd)Text TxtOutside2Tag(Text txtInp, Text tagIni, Text tagEnd)Text TxtOutHtmTag(Text htmTxt)Text TxtOutHtmScr(Text htmTxt)Text TxtReplaceSecuence(Text txtInp, Set repTab)Set TxtForChr(Text inpTxt, Code funChr)Set TxtLineWrap(Text txtInp, Real linMax, Real cmpCtr)Text TxtTwitterWrap(Text txtCut, Text txtNot)//////////////////////////////////////////////////////////////////////////////
Text Q(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Char(34) + txtVal + Char(34) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto entre dobles comillas. Equivalente a la funcion Tol Qt().",
Q);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text W(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Replace(txtVal, "/", "\\") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un camino en formato Unix convertido a formato Windows/DOS.",
W);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text R(Text txtLst) // List of texts separated with |
//////////////////////////////////////////////////////////////////////////////
{ SetGetRand(Txt2Set(txtLst, "|")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto elegido al azar de entre los tokens (|) de otro texto.",
R);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text F(Anything anyVal)
//////////////////////////////////////////////////////////////////////////////
{
Text graVal = Grammar(anyVal);
Case
(
graVal=="Text", anyVal, // Ya es texto
graVal=="Real", If(EQ(anyVal, Round(anyVal)),
FormatReal(anyVal, "%.0lf"), // Entero sin decimales
FormatReal(anyVal, "%.2lf")), // 2 decimales
graVal=="Date", If(Hour(anyVal),
FormatDate(anyVal, "%c%Y/%m/%d %h:%i:%s"), // Tiempo
FormatDate(anyVal, "%c%Y/%m/%d")), // Fecha
graVal=="Set",
{
Real crdSet = Card(anyVal);
Case(
EQ(crdSet,0), "[]",
EQ(crdSet,1), "["+F(anyVal[1])+"]",
TRUE, "["+F(anyVal[1])+SetSum(For(2,crdSet,Text(Real setPos)
{ "|"+F(anyVal[setPos]) }))+"]")
},
TRUE, "Not basic type"
)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna numeros, fechas, textos, conjuntos como un texto de formato simple.",
F);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Txt2Set(Text txtInp, // Texto de entrada
Text sepTok) // Elemento separador
//////////////////////////////////////////////////////////////////////////////
{
Set setSep = TxtTokenizer(txtInp, sepTok);
Set setCmp = EvalSet(setSep, Text(Text eleTxt) { Compact(eleTxt) });
setCmp
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto a partir de un texto txtInp, troceandolo por un separador
sepTok y dependiendo de ctrFun puede, en este orde, si C compactar, si N
eliminar los texto nulos, si U retornar elementos unicos y si S retornar el
set ordenado.",
Txt2Set);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtTokenizer(Text txtInp, // Texto de entrada
Text tagBrk) // Tag por el que se corta
//////////////////////////////////////////////////////////////////////////////
{ Tokenizer(Replace(txtInp, tagBrk, Char(7)), Char(7)) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto de textos resultado de cortar el texto de entrada por un
unico tag tagBrk no incluyendo el tag tagBrk dentro de los textos.
Se soporta en la funcion Tol Tokenizer() que rompe por un unico caracter.
Usa como caracter interno de corte el 7 (bell), esperando que no aparezca.",
TxtTokenizer);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceFirst(Text txtInp, // Input text
Text txtOld, // Old text
Text txtNew) // New text
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, txtOld);
Text result = If(LE(posIni,0), txtInp, // No aparece, no se cambia nada
{
Real lenIni = TextLength(txtOld);
Real posSub = posIni + lenIni;
Real posEnd = TextLength(txtInp);
Sub(txtInp, 1, posIni-1) + txtNew + Sub(txtInp, posSub, posEnd)
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns the result of change, only the very first time, in the input text
txtInp the old text txtOld by the new text txtNew.",
TxtReplaceFirst);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtBetween2Tag(Text inpTxt, // Texto de entrada
Text tagIni, // Tag inicial
Text tagEnd, // Tag final
Real cmpFlg) // Si true aplica Compact()
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(inpTxt, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(inpTxt, tagEnd, posSub);
If(LE(posEnd, 0), "", Sub(inpTxt, posSub, posEnd-1))
});
If(cmpFlg, Compact(result), result)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subtexto entre la primera ocurrencia de tagIni y tagEnd.
Si tagIni o tagEnd no aparecen retorna la tira vacia.
Si cmpFlg es cierto entonces aplica la funcion Compact() al texto que retorna.
Por ejemplo:
TxtBetween2Tag('a b [[ c ]] d [[ e ]] f', '[[', ']]', TRUE)
retorna 'c'.",
TxtBetween2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2TagPlus(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, tagIni);
If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txtInp, tagEnd, posSub);
Real txtLen = TextLength(txtInp);
If(And(EQ(posIni,1),LE(posEnd,0)), txtInp,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txtInp,posIni, txtLen),
Sub(txtInp,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2TagPlus(Sub(txtInp, posEnd+TextLength(tagEnd), txtLen),
tagIni, tagEnd)))
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts between 2 tags plus theis tags (tagIni and tagEnd)
in txtInp.
Is diferente that the classic TxtInside2Tag().
For example: <aaa(::)bbb(---)ccc>, <(>, <)> -> <(::)(---)>.",
TxtInside2TagPlus);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutside2Tag(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Set txtSet = TxtTokenizer(tagIni + tagEnd + txtInp, tagIni);
Set txtCic = EvalSet(txtSet, Text(Text txtTok)
{ TxtBetween2Tag(txtTok + tagIni, tagEnd, tagIni, FALSE) });
SetSum(txtCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts outside 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(:::)ccc>, <(>, <)> -> <aaabbbccc>.",
TxtOutside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmScr(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutHtmTag(TxtOutside2Tag(htmTxt, "<script", "</script>")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html y de los scripts.
Sirve para extraer texto limpio del que sacar palabras clave.",
TxtOutHtmScr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceSecuence(Text txtInp, // Texto de entrada
Set repTab) // Tabla de reemplazamientos
//////////////////////////////////////////////////////////////////////////////
{
Real crdTab = Card(repTab); // Numero de cambios a realizar
If(!crdTab, txtInp, // Nada que hacer
{
Text txtNew = Replace(txtInp, repTab[1][1], repTab[1][2]); // Un cambio
TxtReplaceSecuence(txtNew, SetLastN(repTab,crdTab-1)) // Resto de cambios
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto resultado de aplicar la tabla de reemplazamientos repTab al
texto de entrada txtIno.
Es una version de ReplaceTable(txtInp, repTab, 1) que garantiza la secuencia
de los reemplazamientos del primero al ultimo de los reemplazamientos.
Es una funcion recursiva.
Cada reemplazamiento solo se efectua una vez.",
TxtReplaceSecuence);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtForChr(Text inpTxt, // Texto de entrada
Code funChr) // Funcion tipo Anything(Text oneChr)
//////////////////////////////////////////////////////////////////////////////
{
Real lenTxt = TextLength(inpTxt);
For(1, lenTxt, Anything(Real posTxt) { funChr(Sub(inpTxt,posTxt,posTxt)) })
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna el conjunto resultado de aplicar la funcion funChr(Text oneChr)
a todos los caracteres del texto de entrada txtInp.
Retorna un Set a imagen de las funciones basicas For() y EvalSet().",
TxtForChr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtLineWrap(Text txtInp, // Texto de entrada
Real linMax, // Maximo numero de caracteres por linea
Real cmpCtr) // Si true entonces compacta
//////////////////////////////////////////////////////////////////////////////
{
Text txtCmp = If(cmpCtr, Compact(txtInp), txtInp);
Text txtRev = Reverse(txtCmp);
Real txtLen = TextLength(txtCmp);
Set cutSet = If(LE(txtLen, linMax), [[txtCmp, ""]], // Ya esta hecho
{
Real blkPos = TextFind(txtRev, " ", txtLen-linMax); // Busca para atras
If(GE(blkPos, 1),
{
SetOfText(Sub(txtCmp, 0, txtLen-blkPos),
Sub(txtCmp, txtLen-blkPos+1, txtLen))
},
{
// No se puede cortar
Real blkBad = TextFind(txtCmp, " ", linMax+1); // Busca hacia adelante
If(LT(blkBad, 0), [[txtCmp, ""]], // No hay corte posible
{
SetOfText(Sub(txtCmp, 0, blkBad-1), // Hay un mal corte
Sub(txtCmp, blkBad+1, txtLen))
})
})
});
If(cmpCtr, SetOfText(Compact(cutSet[1]),Compact(cutSet[2])), cutSet)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto de 2 texto el primero con un máximo de linMax caracteres
y el segundo con el resto.
Es el resultado de cortar txtInp por el primer blanco que permita que el corte
cumpla la condición inicial.
Si el texto de entrada es mas corte que linMax retorna un conjunto formado
por el texto inicial y la tira vacia.
Si el corte es imposible busca el mejor corte posible y si no lo encuentra
retorna un conjunto formado por el texto inicial y la tira vacia.
Si cmpCtr es true los resultados son compactados.
Tambien existe en Tol la funcion Wrap() con ciertas semejanzas,
aunque mas a TxtParagraphWrap().",
TxtLineWrap);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtTwitterWrap(Text txtCut, // Texto inicial cortable
Text txtNot) // Texto final no cortable
//////////////////////////////////////////////////////////////////////////////
{
Real chrTwi = 117; // Number of characters for Twitter, teoricamente 140
Set setTwi = TxtLineWrap(txtCut, chrTwi-TextLength(txtNot), TRUE);
Compact(setTwi[1]+txtNot)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto que no supere el maximo de caracteres de Twitter a partir
de un texto inicial que se puede cortar y uno final que no se puede.",
TxtTwitterWrap);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FILE : txt.tol
// AUTHOR : http://www.asolver.com
// PURPOSE : Text functions.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// SHORTNAMES
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text Q(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Char(34) + txtVal + Char(34) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto entre dobles comillas. Equivalente a la funcion Tol Qt().",
Q);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text W(Text txtVal) // Text
//////////////////////////////////////////////////////////////////////////////
{ Replace(txtVal, "/", "\\") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un camino en formato Unix convertido a formato Windows/DOS.",
W);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text R(Text txtLst) // List of texts separated with |
//////////////////////////////////////////////////////////////////////////////
{ SetGetRand(Txt2Set(txtLst, "|")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto elegido al azar de entre los tokens (|) de otro texto.",
R);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text F(Anything anyVal)
//////////////////////////////////////////////////////////////////////////////
{
Text graVal = Grammar(anyVal);
Case
(
graVal=="Text", anyVal, // Ya es texto
graVal=="Real", If(EQ(anyVal, Round(anyVal)),
FormatReal(anyVal, "%.0lf"), // Entero sin decimales
FormatReal(anyVal, "%.2lf")), // 2 decimales
graVal=="Date", If(Hour(anyVal),
FormatDate(anyVal, "%c%Y/%m/%d %h:%i:%s"), // Tiempo
FormatDate(anyVal, "%c%Y/%m/%d")), // Fecha
graVal=="Set",
{
Real crdSet = Card(anyVal);
Case(
EQ(crdSet,0), "[]",
EQ(crdSet,1), "["+F(anyVal[1])+"]",
TRUE, "["+F(anyVal[1])+SetSum(For(2,crdSet,Text(Real setPos)
{ "|"+F(anyVal[setPos]) }))+"]")
},
TRUE, "Not basic type"
)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna numeros, fechas, textos, conjuntos como un texto de formato simple.",
F);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set Txt2Set(Text txtInp, // Texto de entrada
Text sepTok) // Elemento separador
//////////////////////////////////////////////////////////////////////////////
{
Set setSep = TxtTokenizer(txtInp, sepTok);
Set setCmp = EvalSet(setSep, Text(Text eleTxt) { Compact(eleTxt) });
setCmp
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto a partir de un texto txtInp, troceandolo por un separador
sepTok y dependiendo de ctrFun puede, en este orde, si C compactar, si N
eliminar los texto nulos, si U retornar elementos unicos y si S retornar el
set ordenado.",
Txt2Set);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtTokenizer(Text txtInp, // Texto de entrada
Text tagBrk) // Tag por el que se corta
//////////////////////////////////////////////////////////////////////////////
{ Tokenizer(Replace(txtInp, tagBrk, Char(7)), Char(7)) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto de textos resultado de cortar el texto de entrada por un
unico tag tagBrk no incluyendo el tag tagBrk dentro de los textos.
Se soporta en la funcion Tol Tokenizer() que rompe por un unico caracter.
Usa como caracter interno de corte el 7 (bell), esperando que no aparezca.",
TxtTokenizer);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceFirst(Text txtInp, // Input text
Text txtOld, // Old text
Text txtNew) // New text
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, txtOld);
Text result = If(LE(posIni,0), txtInp, // No aparece, no se cambia nada
{
Real lenIni = TextLength(txtOld);
Real posSub = posIni + lenIni;
Real posEnd = TextLength(txtInp);
Sub(txtInp, 1, posIni-1) + txtNew + Sub(txtInp, posSub, posEnd)
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns the result of change, only the very first time, in the input text
txtInp the old text txtOld by the new text txtNew.",
TxtReplaceFirst);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtBetween2Tag(Text inpTxt, // Texto de entrada
Text tagIni, // Tag inicial
Text tagEnd, // Tag final
Real cmpFlg) // Si true aplica Compact()
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(inpTxt, tagIni);
Text result = If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(inpTxt, tagEnd, posSub);
If(LE(posEnd, 0), "", Sub(inpTxt, posSub, posEnd-1))
});
If(cmpFlg, Compact(result), result)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un subtexto entre la primera ocurrencia de tagIni y tagEnd.
Si tagIni o tagEnd no aparecen retorna la tira vacia.
Si cmpFlg es cierto entonces aplica la funcion Compact() al texto que retorna.
Por ejemplo:
TxtBetween2Tag('a b [[ c ]] d [[ e ]] f', '[[', ']]', TRUE)
retorna 'c'.",
TxtBetween2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtInside2TagPlus(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Real posIni = TextFind(txtInp, tagIni);
If(LE(posIni,0), "",
{
Real lenIni = TextLength(tagIni);
Real posSub = posIni + lenIni;
Real posEnd = TextFind(txtInp, tagEnd, posSub);
Real txtLen = TextLength(txtInp);
If(And(EQ(posIni,1),LE(posEnd,0)), txtInp,
If(And(GT(posIni,1),LE(posEnd,0)), Sub(txtInp,posIni, txtLen),
Sub(txtInp,posIni,posEnd+TextLength(tagEnd)-1)+ // Recursion
TxtInside2TagPlus(Sub(txtInp, posEnd+TextLength(tagEnd), txtLen),
tagIni, tagEnd)))
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts between 2 tags plus theis tags (tagIni and tagEnd)
in txtInp.
Is diferente that the classic TxtInside2Tag().
For example: <aaa(::)bbb(---)ccc>, <(>, <)> -> <(::)(---)>.",
TxtInside2TagPlus);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutside2Tag(Text txtInp, // Input text
Text tagIni, // Initial tag
Text tagEnd) // End tag)
//////////////////////////////////////////////////////////////////////////////
{
Set txtSet = TxtTokenizer(tagIni + tagEnd + txtInp, tagIni);
Set txtCic = EvalSet(txtSet, Text(Text txtTok)
{ TxtBetween2Tag(txtTok + tagIni, tagEnd, tagIni, FALSE) });
SetSum(txtCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns all the texts outside 2 tags (tagIni and tagEnd) in txt.
For example: <aaa(::)bbb(:::)ccc>, <(>, <)> -> <aaabbbccc>.",
TxtOutside2Tag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmTag(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutside2Tag(htmTxt, "<", ">") };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html.",
TxtOutHtmTag);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtOutHtmScr(Text htmTxt)
//////////////////////////////////////////////////////////////////////////////
{ TxtOutHtmTag(TxtOutside2Tag(htmTxt, "<script", "</script>")) };
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna todos el texto fuera de los tags de html y de los scripts.
Sirve para extraer texto limpio del que sacar palabras clave.",
TxtOutHtmScr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtReplaceSecuence(Text txtInp, // Texto de entrada
Set repTab) // Tabla de reemplazamientos
//////////////////////////////////////////////////////////////////////////////
{
Real crdTab = Card(repTab); // Numero de cambios a realizar
If(!crdTab, txtInp, // Nada que hacer
{
Text txtNew = Replace(txtInp, repTab[1][1], repTab[1][2]); // Un cambio
TxtReplaceSecuence(txtNew, SetLastN(repTab,crdTab-1)) // Resto de cambios
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto resultado de aplicar la tabla de reemplazamientos repTab al
texto de entrada txtIno.
Es una version de ReplaceTable(txtInp, repTab, 1) que garantiza la secuencia
de los reemplazamientos del primero al ultimo de los reemplazamientos.
Es una funcion recursiva.
Cada reemplazamiento solo se efectua una vez.",
TxtReplaceSecuence);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtForChr(Text inpTxt, // Texto de entrada
Code funChr) // Funcion tipo Anything(Text oneChr)
//////////////////////////////////////////////////////////////////////////////
{
Real lenTxt = TextLength(inpTxt);
For(1, lenTxt, Anything(Real posTxt) { funChr(Sub(inpTxt,posTxt,posTxt)) })
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna el conjunto resultado de aplicar la funcion funChr(Text oneChr)
a todos los caracteres del texto de entrada txtInp.
Retorna un Set a imagen de las funciones basicas For() y EvalSet().",
TxtForChr);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set TxtLineWrap(Text txtInp, // Texto de entrada
Real linMax, // Maximo numero de caracteres por linea
Real cmpCtr) // Si true entonces compacta
//////////////////////////////////////////////////////////////////////////////
{
Text txtCmp = If(cmpCtr, Compact(txtInp), txtInp);
Text txtRev = Reverse(txtCmp);
Real txtLen = TextLength(txtCmp);
Set cutSet = If(LE(txtLen, linMax), [[txtCmp, ""]], // Ya esta hecho
{
Real blkPos = TextFind(txtRev, " ", txtLen-linMax); // Busca para atras
If(GE(blkPos, 1),
{
SetOfText(Sub(txtCmp, 0, txtLen-blkPos),
Sub(txtCmp, txtLen-blkPos+1, txtLen))
},
{
// No se puede cortar
Real blkBad = TextFind(txtCmp, " ", linMax+1); // Busca hacia adelante
If(LT(blkBad, 0), [[txtCmp, ""]], // No hay corte posible
{
SetOfText(Sub(txtCmp, 0, blkBad-1), // Hay un mal corte
Sub(txtCmp, blkBad+1, txtLen))
})
})
});
If(cmpCtr, SetOfText(Compact(cutSet[1]),Compact(cutSet[2])), cutSet)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un conjunto de 2 texto el primero con un máximo de linMax caracteres
y el segundo con el resto.
Es el resultado de cortar txtInp por el primer blanco que permita que el corte
cumpla la condición inicial.
Si el texto de entrada es mas corte que linMax retorna un conjunto formado
por el texto inicial y la tira vacia.
Si el corte es imposible busca el mejor corte posible y si no lo encuentra
retorna un conjunto formado por el texto inicial y la tira vacia.
Si cmpCtr es true los resultados son compactados.
Tambien existe en Tol la funcion Wrap() con ciertas semejanzas,
aunque mas a TxtParagraphWrap().",
TxtLineWrap);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text TxtTwitterWrap(Text txtCut, // Texto inicial cortable
Text txtNot) // Texto final no cortable
//////////////////////////////////////////////////////////////////////////////
{
Real chrTwi = 117; // Number of characters for Twitter, teoricamente 140
Set setTwi = TxtLineWrap(txtCut, chrTwi-TextLength(txtNot), TRUE);
Compact(setTwi[1]+txtNot)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Retorna un texto que no supere el maximo de caracteres de Twitter a partir
de un texto inicial que se puede cortar y uno final que no se puede.",
TxtTwitterWrap);
//////////////////////////////////////////////////////////////////////////////
con-Q.tv construye las páginas del sitio y newsletter Con-Q.tv
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio