2

In my helper app I'm trying to take prototype work in Ipython and quickly turn it into a script (for when it works). I have a working clipboard function thanks to Click button copy to clipboard, on this page:

http://jsfiddle.net/codyc54321/udxp4osm/1/

While it does copy to clipboard, it comes out as a rambling string like

match = re.match(in_rgx, string)match#<_sre.SRE_Match at 0x7f90674cf3d8>match.gromatch.group match.groupdict match.groups match.group()#'In [3]: '

instead of what I see on the page:

match = re.match(in_rgx, string)

match
#<_sre.SRE_Match at 0x7f90674cf3d8>

match.gro
match.group      match.groupdict  match.groups     

match.group()
#'In [3]: '

and there is no newline separation. For some reason my fiddle isn't actually copying, but my app is, but what happens is I used a Django filter to turn my newlines (\n) into <br> tags:

<p id="clean-code">match = re.match(in_rgx, string)<br /><br />match<br />#&lt;_sre.SRE_Match at 0x7f90674cf3d8&gt;<br /><br />match.gro<br />match.group      match.groupdict  match.groups     <br /><br />match.group()<br />#&#39;In [3]: &#39;<br /></p>

and when I hit "copy to clipboard", get that string, with no br tags or newline. So if I paste into Atom, Gedit, any text editor, I get one long line which renders my page useless. I tried putting a real '\n' after each line, and as you can imagine that just added a new line. I tried putting the literal \n text, and then I get:

match = re.match(in_rgx, string)\nmatch\n#<_sre.SRE_Match at 0x7f90674cf3d8>\nmatch.group()\n#'In [3]: '\n

as one line, which makes sense. Here is my current Python with the superfluous \n:

def clean_ipython_line(code_line):
    in_rgx      = r"^In \[\d+\][:] "
    out_rgx     = r"^Out\[\d+\][:] "
    in_match    = re.match(in_rgx, code_line)
    out_match   = re.match(out_rgx, code_line)
    if in_match:
        line = code_line.replace(in_match.group(), '') + '\\n'
        return line
    elif out_match:
        line = ('#' + code_line.replace(out_match.group(), '')) + '\\n'
        return line
    else:
        return code_line
        
def clean_ipython_block(unclean_code):
    unclean_lines = unclean_code.split('\r\n')
    cleaned_lines = []
    for dirty_line in unclean_lines:
        cleaned_lines.append(clean_ipython_line(dirty_line))
    clean_block = "\r\n".join(cleaned_lines)
    print(clean_block)
    return clean_block

Is there a built in JavaScript way to copy to clipboard turning those br tags into the real newline character that will break these lines when I paste into editor?

halfer
  • 19,824
  • 17
  • 99
  • 186
codyc4321
  • 9,014
  • 22
  • 92
  • 165

0 Answers0