PHP DOM DOCUMENT REPLACE DIV to text, REPLACE DIV to DIV - dom

How to replace the div in the HTML page with plain text using the dom document?
How to replace the div in the HTML page with another div using the dom document?

I solved it:
$ Element = $ dom-> getElementById ('id');
$ NodeDiv = $ dom-> createElement ("div", "New content");
$nodeDiv->setAttribute("id", "newid");
$ Element-> parentNode-> replaceChild ($ nodeDiv, $ element);
$ Element = $ dom-> getElementById ('id');
$ NodeText = $ dom-> createTextNode ("New content");
$ Element-> parentNode-> replaceChild ($ nodeText, $ element);

Related

How to use hamlet template with route parameter?

I'm working on a yesod app based on the yesod-postgres stack template. I have a route defined in config/routes that has the form:
foo/edit/#Text EditFooR GET
In my hamlet template, I want to write
<form method=post action=#{EditFooR}#forms enctype=#{formEnctype}>
^{formWidget}
<button type="submit">
Submit
and in my Handler I'd like to write:
getEditFooR :: T.Text -> Handler Html
getEditFooR name = do
....
text <- findTextByName name
(formWidget, formEnctype) <- generateFormPost (editFooForm text)
defaultLayout $ do
$(widgetFile "editFoo")
Except that I need to provide the parameter to the #{EditFooR} route. How is this done in the Hamlet file/Handler?
The answer is that the .hamlet template should have the format:
<form method=post action=#{EditFooR fooName}#forms enctype=#{formEnctype}>
^{formWidget}
<button type="submit">
Submit
and the Handler should have the format:
getEditFooR :: T.Text -> Handler Html
getEditFooR name = do
....
text <- findTextByName name
fooName <- "something or other"
(formWidget, formEnctype) <- generateFormPost (editFooForm text)
defaultLayout $ do
$(widgetFile "editFoo")

Examples of using SCons with knitr

Are there minimal, or even larger, working examples of using SCons and knitr to generate reports from .Rmd files?
kniting an cleaning_session.Rmd file from the command line (bash shell) to derive an .html file, may be done via:
Rscript -e "library(knitr); knit('cleaning_session.Rmd')".
In this example, Rscript and instructions are fed to a Makefile:
RMDFILE=test
html :
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''
In this answer https://stackoverflow.com/a/10945832/1172302, there is reportedly a solution using SCons. Yet, I did not test enough to make it work for me. Essentially, it would be awesome to have something like the example presented at https://tex.stackexchange.com/a/26573/8272.
[Updated] One working example is an Sconstruct file:
import os
environment = Environment(ENV=os.environ)
# define a `knitr` builder
builder = Builder(action = '/usr/local/bin/knit $SOURCE -o $TARGET',
src_suffix='Rmd')
# add builders as "Knit", "RMD"
environment.Append( BUILDERS = {'Knit' : builder} )
# define an `rmarkdown::render()` builder
builder = Builder(action = '/usr/bin/Rscript -e "rmarkdown::render(input=\'$SOURCE\', output_file=\'$TARGET\')"',
src_suffix='Rmd')
environment.Append( BUILDERS = {'RMD' : builder} )
# define source (and target files -- currently useless, since not defined above!)
# main cleaning session code
environment.RMD(source='cleaning_session.Rmd', target='cleaning_session.html')
# documentation of the Cleaning Process
environment.Knit(source='Cleaning_Process.Rmd', target='Cleaning_Process.html')
# documentation of data
environment.Knit(source='Code_Book.Rmd', target='Code_Book.html')
The first builder calls the custom script called knit. Which, in turn, takes care of the target file/extension, here being cleaning_session.html. Likely the suffix parameter is not needed altogether, in this very example.
The second builder added is Rscript -e "rmarkdown::render(\'$SOURCE\')"'.
The existence of $TARGETs (as in the example at Command wrapper) ensures SCons won't repeat work if a target file already exists.
The custom script (whose source I can't retrieve currently) is:
#!/usr/bin/env Rscript
local({
p = commandArgs(TRUE)
if (length(p) == 0L || any(c('-h', '--help') %in% p)) {
message('usage: knit input [input2 input3] [-n] [-o output output2 output3]
-h, --help to print help messages
-n, --no-convert do not convert tex to pdf, markdown to html, etc
-o output filename(s) for knit()')
q('no')
}
library(knitr)
o = match('-o', p)
if (is.na(o)) output = NA else {
output = tail(p, length(p) - o)
p = head(p, o - 1L)
}
nc = c('-n', '--no-convert')
knit_fun = if (any(nc %in% p)) {
p = setdiff(p, nc)
knit
} else {
if (length(p) == 0L) stop('no input file provided')
if (grepl('\\.(R|S)(nw|tex)$', p[1])) {
function(x, ...) knit2pdf(x, ..., clean = TRUE)
} else {
if (grepl('\\.R(md|markdown)$', p[1])) knit2html else knit
}
}
mapply(knit_fun, p, output = output, MoreArgs = list(envir = globalenv()))
})
The only thing, now, necessary is to run scons.

Trying to comment some lines from dovecot conf file

I'm trying to comment this lines:
passdb {
driver = pam
[session=yes] [setcred=yes] [failure_show_msg=yes] [max_requests=<n>]
[cache_key=<key>] [<service name>]
args = dovecot
}
via sed:
sed -i '1!N; s/passdb {\
driver = pam\
\[session=yes\] \[setcred=yes\] \[failure_show_msg=yes\] \[max_requests=\<n\>\]\
\[cache_key=\<key\>\] \[\<service name\>\]\
args = dovecot\
}/#passdb {\
# driver = pam\
# [session=yes] [setcred=yes] [failure_show_msg=yes] [max_requests=<n>]\
# [cache_key=<key>] [<service name>]\
# args = dovecot\
#}/' t
But it doesn't match what I need, can anyone tell me what I'm doing wrong here?
If all you are trying to do is comment the lines between passdb and }, then the following should suffice
sed -i '/^passdb {/,/}/s/^/#/g' file
Using awk
awk '/^passdb {/,/^}/ {$0="#"$0}1' file

Change epydoc's favicon - how to?

Everything is in the question :
How would I do to change epydoc's generated html pages favicon ?
Note : I saw how to customize the css content, but nothing about customizing output html files...
Many thanks.
Ok, so after a littel brainstorming (with myself), I found a way :
Since we can inject some html using --navlink option to epydoc (initially designed to customize project's link in the navigation bar), I used a javascript trick to add the following dynamically in the tag of the document.
Here is the javascript code :
var link = parent.document.createElement('link');
link.id = 'dynamic-favicon';
link.type = 'image/png';
link.rel = 'shortcut icon';
link.href = '../logo-fav.png'; parent.document.head.appendChild(link);
parent.document.head = parent.document.head ||
parent.document.getElementsByTagName('head')[0];
And here is the full epydoc command line :
epydoc -v --name "My Project" -o ./html \
--css epydoc.css --url http://www.my-project.org --inheritance listed \
--graph all --no-private --docformat epytext \
--navlink "<img src=\"../logo.png\" style=\"margin:10px;\" />
<script>
var link = parent.document.createElement('link');
link.id = 'dynamic-favicon';
link.type = 'image/png';
link.rel = 'shortcut icon';
link.href = '../logo-fav.png';
parent.document.head.appendChild(link);
parent.document.head = parent.document.head || parent.document.getElementsByTagName('head')[0];
</script>
" \
my_py_module
This can be used to customize the whole documentation, but is still hacky. Still strange that their is no way to use some templates in such a mature tool like epydoc...

Coffeescript compilation: not as file2file, but as text2text

We can compile coffescript file to js-file with command:
coffee --join path/to/result.js --compile path/to/coffeescript_dir/
But what if I want to compile a piece of coffeescript code (as text) and get piece of js code (as a text too), and they are not files.
For example:
cs text: "func = () -> 55"
js text result: "var func; func = function(){return 55;}"
It must be done from console, or even better from python interactive console :)
You can use --eval to take a string parameter as coffee input, --bare to avoid the JS output being wrapped in a closure, and --print to print the output on stdout instead of a file:
$ coffee --print --bare -eval 'func = -> 55'
var func;
func = function() {
return 55;
};
To call it from Python, you can use the subprocess module:
from subprocess import Popen, PIPE
def compile_cs(cs_code):
args = ['coffee', '--print', '--bare', '--eval', cs_code]
return Popen(args, stdout=PIPE).communicate()[0]