Generalized Quine Generator The Challenge

In this challenge, you specify a source language S and a target language T. Your task is to write the following program P in the language S. If a valid program Q in the language T is given as input to P, it will output a valid program R in the language T which takes no input and outputs Q(R), that is, the program Q applied to the source code of R. In addition, you should present in your answer a nontrivial example program Q (the more interesting, the better, although you score no points for this), the resulting program R, and the output of R. This is code-golf, so the shortest code for P wins.

In other words, this is a challenge about writing a "universal quine constructor" that can create arbitrary types of generalized quines.

Clarifications


Your source and target languages may be identical.
The program P should take one string as input (from STDIN or equivalent), and output one string (to STDOUT or equivalent), as should every output program R.
The input programs Q should also transform a string to another string, but their form is more flexible: they can be string-to-string functions, code snippets that modify a variable with a certain name, snippets that modify the data stack if your target language has one, etc. You can also further restrict the form of the Q's by stating that, for example, they may not contain any comments. However, you must be able to implement any computable string-to-string function as an input program Q, and you must explicitly state how they function and what further constraints you place on them.
The output program R should really be a (generalized) quine, so it must not read any input (user input, files etc.) unless Q does so.
Standard loopholes are disallowed.


An Example

Suppose I choose Python as my source language, and Haskell as my target language, and I further require that the input program should be a one-line definition of a String -&gt; String function named f. If I give the string-reversing program

f x = reverse x


as input to my Python program P, it will output the source code of another Haskell program R. This program prints to STDOUT the source code of R, but reversed. If P is given the identity function

f x = x


as input, the output program R is a quine.
Source = Target = CJam, 19 17 16 bytes

{`"_~"+}`)q\"_~"


This assumes that the input program Q (given on STDIN) is some CJam snippet which expects a string on the top of the stack and leaves another string on top of the stack.

Test it here.

Examples


The identity would just be an empty snippet, so leaving STDIN empty prints 

{`"_~"+}_~


Which is the standard quine, with an additional +.
To reverse a string in CJam, you can use W%, so putting that on STDIN, this yields:

{`"_~"+W%}_~


which we can run to obtain

~_}%W+"~_"`{

As a third example, say we use a snippet which intersperses a string with spaces: ' *. Running P with that as input, we get

{`"_~"+' *}_~


which in turn prints

{ ` " _ ~ " + '   * } _ ~  

It now also works if Q contains line breaks (although that's never necessary in CJam). Here is a program with a line break, which removes all line breaks from a string (in an unnecessarily convoluted way - split into lines, then join):

N/
""
*


This results in the following R:

{`"_~"+N/
""
*}_~


which in turn prints

{`"_~"+N/""*}_~



Explanation

Let's look at the produced output first:

The standard CJam quine is

{`"_~"}_~


It works as follows:


Push the block {`"_~"}.
Duplicate it with _.
Execute the copy with ~.
Now inside the block, ` turns the first block into its string representation.
"_~" pushes the two characters of the source that are not part of the block (and hence missing from the string representation).
The two strings are printed back to back at the end of the program.


In the basic quine the ` is unnecessary, because if you just leave the block as it is, it's printed all the same at the end of the program.

The output of my program P is a modified version of this snippet. First, I've added a + to the block, which concatenates the two strings into one string containing the entire source. Note that this will be true no matter what I do inside the block, because that will all get added to the string representation obtained with `. Now I can simply put the program/snippet Q inside the block after the +, so that it can modify the source string before it is printed. Again, since Q goes inside the block, it will be part of said source string.

In summary, P prints

{`"_~"+Q}_~


Now, for how I go about constructing this output in P:

{`"_~"+}         "Push the block without Q.";
        `        "Turn it into a string. This is shorter than writing a string right away,
                  because I'd have to escape the quotes, and I'd need two quotes instead of
                  one backtick.";
         )       "Pop off the last character (the brace) and push it on the stack.";
          q      "Read input Q.";
           \     "Swap Q with the brace.";
            "_~" "Push the final two characters.";


The four strings are printed automatically (back-to-back) at the end of the program.