How do I parse wikitext using built-in mediawiki support for lua scripting? - lua

The wiktionary entry for faint lies at https://en.wiktionary.org/wiki/faint
The wikitext for the etymology section is:
From {{inh|en|enm|faynt}}, {{m|enm|feynt||weak; feeble}}, from
{{etyl|fro|en}} {{m|fro|faint}}, {{m|fro|feint||feigned; negligent;
sluggish}}, past participle of {{m|fro|feindre}}, {{m|fro|faindre||to
feign; sham; work negligently}}, from {{etyl|la|en}}
{{m|la|fingere||to touch, handle, usually form, shape, frame, form in
thought, imagine, conceive, contrive, devise, feign}}.
It contains various templates of the form {{xyz|...}}
I would like to parse them and get the text output as it shows on the page:
From Middle English faynt, feynt (“weak; feeble”), from Old French
faint, feint (“feigned; negligent; sluggish”), past participle of
feindre, faindre (“to feign; sham; work negligently”), from Latin
fingere (“to touch, handle, usually form, shape, frame, form in
thought, imagine, conceive, contrive, devise, feign”).
I have about 10000 entries extracted from the freely available dumps of wiktionary here.
To do this, my thinking is to extract templates and their expansions (in some form). To explore the possibilites I've been fiddling with the lua scripting facility on mediawiki. By trying various queries inside the debug console on edit pages of modules, like here:
https://en.wiktionary.org/w/index.php?title=Module:languages/print&action=edit
mw.log(p)
>> table
mw.logObject(p)
>> table#1 {
["code_to_name"] = function#1,
["name_to_code"] = function#2,
}
p.code_to_name("aaa")
>>
p.code_to_name("ab")
>>
But, I can't even get the function calls right. p.code_to_name("aaa") doesn't return anything.
The code that presumably expands the templates for the etymology section is here:
https://en.wiktionary.org/w/index.php?title=Module:etymology/templates
How do I call this code correctly?
Is there a simpler way to achieve my goal of parsing wikitext templates?
Is there some function available in mediawiki that I can call like "parse-wikitext("text"). If so, how do I invoke it?

To expand templates (and other stuff) in wikitext, use frame.preprocess, which is called as a method on a frame object. To get a frame object, use mw.getCurrentFrame. For instance, type = mw.getCurrentFrame():preprocess('{{l|en|word}}') in the console to get the wikitext resulting from {{l|en|word}}. That currently gives <span class="Latn" lang="en">[[word#English|word]]</span>.
You can also use the Expandtemplates action in the MediaWiki API ( https://en.wiktionary.org/w/api.php?action=expandtemplates&text={{l|en|word}}), or the Special:ExpandTemplates page, or JavaScript (if you open the browser console while browsing a Wiktionary page):
new mw.Api().get({
action: 'parse',
text: '{{l|en|word}}',
title: mw.config.values.wgPageName,
}).done(function (data) {
const wikitext = data.parse.text['*'];
if (wikitext)
console.log(wikitext);
});
If the mw.api library hasn't already been loaded and you get a TypeError ("mw.Api is not a constructor"):
mw.loader.using("mediawiki.api", function() {
// Use mw.Api here.
});
So these are some of the ways to expand templates.

Related

Acumatica Customization: Add PO Nbr column to SO Line

I am trying to create a customization to add the PO Nbr associated in the PO Link graph to a column in the SO Line. I know the link is made in the SOLineSplit table, but I am struggling with how to actually show that field in the SO Line. I have made a custom Usr field for PO Nbr, but not sure what I should be entering in the attributes (or DAC extension, whatever is the best way) in order to show the PO Nbr data from the SOLineSplit table.
I remember struggling with this early on, so it's an excellent question. The challenge is that you mention getting to the PO from the SO LINE, but an SO Line can be split across multiple PO's. That's why you end up having to dig into the SOLineSplit for the link. Let's take a look at the link from SOLineSplit to PO...
There are a couple of possibilities when you look into the SOOrderEntry graph. The following code snippets show relationships. Your SOLineSplit may have a direct reference to the PO/POLine as shown here:
PXSelectBase<POLine> selectlinkedDropShips = new PXSelectJoin<POLine,
InnerJoin<SOLineSplit, On<SOLineSplit.pOType, Equal<POLine.orderType>,
And<SOLineSplit.pONbr, Equal<POLine.orderNbr>,
And<SOLineSplit.pOLineNbr, Equal<POLine.lineNbr>>>>>,
Where<SOLineSplit.orderType, Equal<Current<SOOrder.orderType>>,
And<SOLineSplit.orderNbr, Equal<Current<SOOrder.orderNbr>>,
And<POLine.orderType, Equal<POOrderType.dropShip>>>>>(this);
If that simple path doesn't get you there, the more detailed path of an SO to a PO lies in the INItemPlan reflecting the demand of the SOLineSplit (via the PlanID) and then tied to POLine via POLine.PlanID = INItemPlan.SupplyPlanID as shown in the following snippet.
foreach (PXResult<POLine, POOrder, INItemPlan, SOLineSplit> res in PXSelectJoin<POLine,
InnerJoin<POOrder, On<POLine.FK.Order>,
InnerJoin<INItemPlan, On<INItemPlan.supplyPlanID, Equal<POLine.planID>>,
InnerJoin<SOLineSplit, On<SOLineSplit.planID, Equal<INItemPlan.planID>,
And<SOLineSplit.pOType, Equal<POLine.orderType>,
And<SOLineSplit.pONbr, Equal<POLine.orderNbr>,
And<SOLineSplit.pOLineNbr, Equal<POLine.lineNbr>>>>>>>>,
Where<POLine.orderType, Equal<Required<POLine.orderType>>,
And<POLine.orderNbr, Equal<Required<POLine.orderNbr>>,
And2<Where<POLine.cancelled, Equal<boolTrue>,
Or<POLine.completed, Equal<boolTrue>>>,
And2<Where<POOrder.orderType, NotEqual<POOrderType.dropShip>,
Or<POOrder.isLegacyDropShip, Equal<True>>>,
And<SOLineSplit.receivedQty, Less<SOLineSplit.qty>,
And<SOLineSplit.pOCancelled, NotEqual<boolTrue>,
And<SOLineSplit.completed, NotEqual<boolTrue>>>>>>>>>
.Select(graph, poOrder.OrderType, poOrder.OrderNbr))
{...
As you can imagine, you would have to set some rules around restricting an SO Line to a single PO, but that honestly would be more trouble than it's worth as the standard functionality is valuable in being more robust. I would suggest either putting your link in the Allocations list (or Line Details in more recent versions of Acumatica ERP) or produce a smart panel popup to show all the PO links per splits of the current SO Line.

Dart Markdown package, how to handle new lines

I am trying to make a WYSIWYG internal tool. And we decided to implement this feature with contentEditable. However, we save data to our databases in markdown. So I have to be able to parse from html to md and back. For html to md I use package html2md and for the other way around I use Markdown package.
The issue i've been having is that when you write to my editor text like
HEY
After many lines some text
It produces this in md
HEY
After many lines some text
Notably it uses 2 whitespace and 2 LF characters (or atleast i think so but i might be slightly wrong.) I solved this issue by parsing it like this
markdownToHtml(data.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'), inlineSyntaxes: [TextSyntax(String.fromCharCodes([32,32,10,10]),sub: "<div><br></div>")],inlineOnly: true );
The inline only parameter was neccesary because without it the text syntax wasnt applied for some reason. However this inline only then bit me in the arse when I tried to implement parsing of unordered lists, which are parsed as blocks. So I need a way to correctly parse these empty lines without using inline only.
class EmptyLineBlockSyntax extends BlockSyntax{
RegExp get pattern => RegExp(r'^(?:[ \t][ \t]+)$');
const EmptyLineBlockSyntax();
Node parse(BlockParser parser) {
parser.encounteredBlankLine = true;
parser.advance();
return Element('p',[Element.empty('br')]);
}
}
return markdownToHtml(data.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>'), blockSyntaxes: [EmptyLineBlockSyntax()]);

Prestashop all translatable-field display none for product page

Just new in Prestashop (1.6.0.6), I've a problem with my product page in admin. All translatable-field are to display:none (I inspect the code with chrome).
So when I want to create a new product I can't because the name field is required.
I thought that it was simple to find the .js whose do that but it isn't.
If somebody could help me, I would be happy.
Thank you for your help
Hi,
I make some searches and see that the function hideOtherLanguage(id) hide and show translatable-field element.
function hideOtherLanguage(id)
{
console.log(id_language);
$('.translatable-field').hide();
$('.lang-' + id).show();
var id_old_language = id_language;
id_language = id;
if (id_old_language != id)
changeEmployeeLanguage();
updateCurrentText();
}
When I set the Id to 1 (default language), it works. It seems that when I load the page, the function is called twice and the last calling, the id value is undefined. So the show() function will not work.
If somebody could help me. Thank you.
In my console, I see only one error
undefined is not a function.
under index.php / Line 1002
...
$("#product_form").validate({
...
But I find the form.tpl template and set this lines in comment but nothing change.
EDIT: According to comment on this link http://forge.prestashop.com/browse/PSCFV-2928 this can possibly be caused by corrupted installation file(s) - so when on clean install - try to re-download and reinstall...
...otherwise:
I got into a similar problem - in module admin page, when creating configuration form using PrestaShop's HelperForm. I will provide most probable cases and their possible solutions.
The solution for HelperForm was tested on PS 1.6.0.14
Generally there are 2 cases when this will happen.
First, you have to check what html you recieve.
=> Display source code - NOT in developer tools/firebug/etc...!
=> I really mean the pure recieved (JavaScript untouched) html.
Check if your translatable-fields have already the inline style "display: none":
Case 1 - fields already have inline style(s) for "display: none"
This means the template/html was already prepared this way - most probably in some TPL file I saw codes similar to these:
<div class="translatable-field lang-{$language.id_lang}"
{if $language.id_lang != $id_lang_default}style="display:none"{/if}>
Or particularly in HelperForm template:
<div class="translatable-field lang-{$language.id_lang}"
{if $language.id_lang != $defaultFormLanguage}style="display:none"{/if}>
Case 1 is the most easy to solve, you just have to find, where to set this default language.
Solutions
HelperForm
Look where you've (or someone else) prepared the HelperForm object - something like:
$formHelper = new HelperForm();
...
Somewhere there will be something like $formHelper->default_form_language = ...;
My wrong first solution was to get default form language from context - which might not be set:
$this->context->controller->default_form_language; //THIS IS WRONG!
The correct way is to get the default language from configuration - something like:
$default_lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$formHelper->default_form_language = $default_lang->id;
...this particularly solved my problem...
Other form-creations
If there is something else than HelperForm used for form creations, the problem is still very similar.
You have to find where in files(probably tpls) is a condition for printing display:none for your case - then find where is the check-against-variable set and set it correctly yourself.
Case 2 - fields don't have inline style(s) for "display: none"
This means it is done after loading HTML by JavaScript. There are two options:
There is a call for hideOtherLanguage(), but there is wrongly set input language - that means no language will be displayed and all hidden.Solution for this one can be often solved by solving Case 1 (see above). In addition there can be programming error in not setting the after-used language id variable at all... then you would have to set it yourself (assign in JavaScript).
Some script calls some sort of .hide() on .translatable-field - you will have to search for it the hard way and remove/comment it out.
PS: Of course you can set the language to whatever you want, it is just common to set it to default language, because it is the most easier and the most clear way how to set it.

Using go-html-transform to preprocess HTML: Replace fails

Following on from this question on whitelisting HTML tags, I've been experimenting with Jeremy Wall's go-html-transform. In the hopes of improving searchable documentation I'm asking this here rather than pestering the author directly... hopefully this isn't too tool-specific for SO.
App Engine, latest SDK. Post.Body is a []byte. This works:
package posts
import (
// ...
"html/template"
"code.google.com/p/go-html-transform/html/transform"
"code.google.com/p/go-html-transform/h5"
)
// ...
// Pre-process post body, then return it to the template as HTML()
// to avoid html/template's escaping allowable tags
func (p *Post) BodyHTML() template.HTML {
doc, _ := transform.NewDoc(string(p.Body))
t := transform.NewTransform(doc)
// Add some text to the end of any <strong></strong> nodes.
t.Apply(transform.AppendChildren(h5.Text("<em>Foo</em>")), "strong")
return template.HTML(t.String())
}
Result:
<strong>Blarg.<em>Foo</em></strong>
However, if instead of AppendChildren() I use something like the following:
t.Apply(transform.Replace(h5.Text("<em>Foo</em>")), "strong")
I get an internal server error. Have I misunderstood the use of Replace()? The existing documentation suggests this sort of thing should be possible.
Running your transform code outside of App Engine, it panics and you can see a TODO in the source at that point. Then it's not too much harder to read the code and see that it's going to panic if given a root node.

How to get specific information from the website and display it in a box?

I am quite newbie on AutoIT, I've done alot of things already.
But I can't find out the way to read specific information from the website that is between div or something. If thats not possible, maybe there is a way to read a raw *.txt(or php) file from the website?
You can use the following code example for your URL:
#include <IE.au3>
$oIE = _IECreate("http://test.com/check.php")
$divs = _IETagNameGetCollection($oIE, "div")
For $div In $divs
If $div.className == "content-container" Then
MsgBox(0, "Bestimmter Inhalt", $div.innerText)
EndIf
Next
_IEQuit($oIE)
You asked for the "raw Text", which is best read with the attribute innerText. See MSDN for more details.
This example basically opens a new IE window with the given URL. Then it extracts all the div-Elements and iterates through them in a loop. Once the className is "content-container" the content of this element is displayed in a message box. Finally the IE window is closed again.
You could add the parameters , 0, 0 to the _IECreate() function to hide the browser window in the end.
Instead of searching for a specific <div>-Element as shown here you could just use the following two lines...
$body = _IETagNameGetCollection($oIE, "body", 0)
MsgBox(0, "Komplette Webseite", $body.innerText)

Resources