Generalized Quine Generator
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.