How can I copy code from Kindle PC and preserve formatting
Asked Answered
P

3

12

Has anyone figured out a good way to copy code from Kindle PC?

I've just downloaded a programming book to Kindle PC for the first time. The code in the book is formatted very nicely, and I'd like to copy the code directly from the Kindle PC book to Visual Studio without having to use auto-indent. For some reason when I copy text from the book, none of the whitespace, tabs, or newlines are preserved. I literally end up with this:

using System; using System.Collections.Generic; using System.Linq; namespace NumericQuery { class Program { static void Main(string[] args) { List list = new List() { 1, 2, 3 }; var query = from number in list where number < 3 select number; foreach (var number in query) { Console.WriteLine(number); } } } }

I'm frustrated because I can't download the code from the author's website since I don't have an ISBN (because I didn't buy a hardcopy).

Peck answered 27/9, 2011 at 8:23 Comment(3)
The ISBN number is the same for everybody.Obeded
In most cases the ASIN in the URL is the ISBN (which is listed on the page anyway). This isn't guaranteed (sometimes there are collisions, usually older books where a publisher reused an ISBN from an out-of-print book) but it's correct most of the time.Corelli
Can't copy paste text ?! This is so absurd. Why do honest people always have to pay for others ?Clannish
D
6

I have just resolved this issue of copying code snippets from a Kindle book to a text/source code editor of your choice. This same topic was discussed in a posting on stackoverflow.com entitled "Why is Chrome rendering this HTML as a completely blank page? From Node.js, following The Node Beginner Book [closed]". That particular posting describes the exact same problem I was experiencing (same Kindle book, same code snippet, same code symptom!). Unfortunately, that posting was prematurely closed before any of the respondents could provide the exact answer, otherwise I would've responded to that posting.

However, I dug into this issue more deeply and discovered the root cause of the problem when copying code snippets from Kindle books: when you copy text from the Kindle app, it uses hex code 0xA0 for space characters, not 0x20. Hex code 0xA0 is extended ASCII for non-breaking whitespace. Well, this doesn't work when you are expecting to copy-and-paste HTML literal strings, as was the case in the aforementioned posting.

And so this explains the behavior in the aforementioned posting: the original poster indicated that he could get around the problem by hand-retyping all of the text. It's because the hand re-typing was using proper 0x20.

This had other symptoms which I didn't understand at first but are now explained: my text editor (Notepad++) was not correctly identifying the reserved keywords in my source code. Again, this is because the keywords were separated by 0xA0, not 0x20. The keyword parser in Notepad++ must be tokenizing off of 0x20.

Solution: after pasting text from Kindle, perform a search and replace using regular expression searching capabilities in your source code editor. Search for regular expression \xA0 and replace it with \x20 (or, depending on your editor just type in a single space bar character in the Replace field [that is how Notepad++ works]).

Doretha answered 23/5, 2013 at 20:47 Comment(0)
C
0

I would check the publisher's website. I've found that they will often post the source code that accompanies books. If they don't do this, then it makes me much less likely to buy the book. (In some cases, no amount of copy/pasting can retrieve any code, regardless of poor formatting, and so trying to get the code from the publisher is the only way).

Chloral answered 9/6, 2016 at 4:28 Comment(0)
W
0

I found that Kindle for PC was simply converting all the newlines to \x20 making the find and replace trick not work. My solution isn't great, but it works:

I downloaded Calibre and started using that. Suffice it to say, you can find ways to use Calibre to open whatever you need it to with some Googling.

Whitehurst answered 16/2, 2017 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.