Hi all.
I am attempting to store the Wikimedia Lua Modules as Classes in my image. The idea being that when the Parser encounters a Module #invoke I can hand it off to the appropriate class and have it invoke Lua and return content to me.
I have extracted the Wikimedia Modules to a postgres table named 'modules' and I am hitting that table and iterating over it to dynamically create the classes I need.
The code is a modified version of what I used to scrape the Squeak Wiki.
scrapeModulesToNewClass
|rows |
rows := ((WikitextParserConnectionPool default executeQuery: 'select page_id, page_title, old_text from modules limit 2') at:1) rows.
rows do: [:each | |pageid pagetitle oldtext lua methodTemplatePageId methodTemplatePageTitle methodTemplateLua methodSourcePageId methodSourcePageTitle methodSourceLua newclassname|
pageid := each at:1.
pagetitle := each at:2.
oldtext := each at:3.
lua := oldtext . "I have been attempting String shenanigans here in attempts to get this to work"
lua inspect.
methodTemplatePageId := 'pageId ^', $', '{1}' ,$'.
methodTemplatePageTitle := 'pageTitle ^', $', '{1}' ,$'.
methodTemplateLua := 'lua ^', $', '{1}' ,$'.
methodSourcePageId := methodTemplatePageId format:{pageid}.
methodSourcePageTitle := methodTemplatePageTitle format:{pagetitle}.
methodSourceLua := methodTemplateLua format:{lua}.
newclassname := ('WikitextModule', pageid asString) asSymbol.
WikitextParserModule subclass: newclassname
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'WikitextParser-Modules'.
(Smalltalk at: newclassname) compile: methodSourcePageId.
(Smalltalk at: newclassname) compile: methodSourcePageTitle.
(Smalltalk at: newclassname) compile: methodSourceLua.
"Transcript show: newclassname; cr."
]
Works fine until
(Smalltalk at: newclassname) compile: methodSourceLua.
Here's why:
lua ^'-- This module provides easy processing of arguments passed to Scribunto from
-- #invoke. It is intended for use by other Lua modules, and should not be
-- called from #invoke directly.
local libraryUtil = require('libraryUtil End of block expected ->')
local checkType = libraryUtil.checkType
..........
end
return arguments'
The Method compilation fails.
Should I even attempt to escape this thing?
Remember, I am going to have to do an OSProcess lua call to run this stuff.
I suppose I could just store the PGRow in the class, but that feels weird.
Thanks in advance.
Hi,
Without having thought it through that much or having understood the whole context:
1. Can't you just replace each ' in your Lua text by '' (two single quotes) before compiling the Smalltalk method string? It escapes the single quotes in the string. 2. Do you really need to create a new Smalltalk class for each row? (That feels weird to me.) Why not create an instance of something for each row and store the collection of all the instances in a well-known place? These instances could have an instance variable to hold the plain Lua code string. 3. If you need Smalltalk classes, you could as well declare a class instance variable and store the unmodified Lua code string there.
YourClass class instanceVariableNames: 'lua'.
lua: aString "class side" lua := aString.
lua ^ lua
lua "instance side" ^ self class lua
YourClass lua: (row at: 3).
Kind regards, Jakob
Am Mi., 21. Aug. 2019 um 22:49 Uhr schrieb gettimothy via Squeak-dev < squeak-dev@lists.squeakfoundation.org>:
Hi all.
I am attempting to store the Wikimedia Lua Modules as Classes in my image. The idea being that when the Parser encounters a Module #invoke I can hand it off to the appropriate class and have it invoke Lua and return content to me.
I have extracted the Wikimedia Modules to a postgres table named 'modules' and I am hitting that table and iterating over it to dynamically create the classes I need. The code is a modified version of what I used to scrape the Squeak Wiki.
scrapeModulesToNewClass |rows | rows := ((WikitextParserConnectionPool default executeQuery: 'select page_id, page_title, old_text from modules limit 2') at:1) rows.
rows do: [:each | |pageid pagetitle oldtext lua methodTemplatePageId methodTemplatePageTitle methodTemplateLua methodSourcePageId methodSourcePageTitle methodSourceLua newclassname|
pageid := each at:1. pagetitle := each at:2. oldtext := each at:3. lua := oldtext . "I have been attempting String shenanigans here in attempts to get this to work" lua inspect. methodTemplatePageId := 'pageId ^', $', '{1}' ,$'. methodTemplatePageTitle := 'pageTitle ^', $', '{1}' ,$'. methodTemplateLua := 'lua ^', $', '{1}' ,$'. methodSourcePageId := methodTemplatePageId format:{pageid}. methodSourcePageTitle := methodTemplatePageTitle format:{pagetitle}. methodSourceLua := methodTemplateLua format:{lua}.
newclassname := ('WikitextModule', pageid asString) asSymbol. WikitextParserModule subclass: newclassname instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'WikitextParser-Modules'.
(Smalltalk at: newclassname) compile: methodSourcePageId. (Smalltalk at: newclassname) compile: methodSourcePageTitle. (Smalltalk at: newclassname) compile: methodSourceLua. "Transcript show: newclassname; cr."
]
Works fine until
(Smalltalk at: newclassname) compile: methodSourceLua.
Here's why:
lua ^'-- This module provides easy processing of arguments passed to Scribunto from -- #invoke. It is intended for use by other Lua modules, and should not be -- called from #invoke directly.
local libraryUtil = require('libraryUtil *End of block expected -*>*'*) local checkType = libraryUtil.checkType
..........
end
return arguments'
The Method compilation fails.
Should I even attempt to escape this thing?
Remember, I am going to have to do an OSProcess lua call to run this stuff.
I suppose I could just store the PGRow in the class, but that feels weird.
Thanks in advance.
squeak-dev@lists.squeakfoundation.org