如何从 Kindle PC 复制代码并保留格式
有人找到了从 Kindle PC 复制代码的好方法吗?
我刚刚第一次将编程书籍下载到 Kindle PC。书中的代码格式非常好,我想将代码直接从 Kindle PC 书中复制到 Visual Studio,而无需使用自动缩进。由于某种原因,当我从书中复制文本时,没有保留任何空格、制表符或换行符。我最终得到的结果是:
使用系统;使用 System.Collections.Generic;使用 System.Linq;命名空间 NumericQuery { 类 Program { static void Main(string[] args) { List list = new List() { 1, 2, 3 }; var query = 来自列表中的数字,其中 number < 3 选择号码; foreach (查询中的变量编号) { Console.WriteLine(number); 我很沮丧,因为
我无法从作者的网站下载代码,因为我没有 ISBN(因为我没有购买硬拷贝)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚解决了将代码片段从 Kindle 图书复制到您选择的文本/源代码编辑器的问题。 stackoverflow.com 上的一篇题为“为什么 Chrome 将此 HTML 渲染为完全空白的页面?来自 Node.js,遵循 Node Beginner Book [已关闭]”的帖子讨论了同一主题。那个特定的帖子描述了我遇到的完全相同的问题(同一本 Kindle 书,相同的代码片段,相同的代码症状!)。不幸的是,在任何受访者能够提供确切答案之前,该帖子已过早关闭,否则我会回复该帖子。
然而,我更深入地研究了这个问题,并在从 Kindle 书籍复制代码片段时发现了问题的根本原因:当您从 Kindle 应用程序复制文本时,它使用十六进制代码 0xA0 来表示空格字符,而不是 0x20。十六进制代码 0xA0 是不间断空白的扩展 ASCII。好吧,当您希望复制并粘贴 HTML 文字字符串时,这不起作用,就像前面提到的帖子中的情况一样。
因此,这解释了上述帖子中的行为:原始发帖者表示他可以通过手动重新输入所有文本来解决该问题。这是因为手动重新输入使用了正确的 0x20。
这还有其他症状,我一开始不明白,但现在得到了解释:我的文本编辑器(Notepad++)无法正确识别源代码中的保留关键字。同样,这是因为关键字是用 0xA0 分隔的,而不是 0x20。 Notepad++ 中的关键字解析器必须对 0x20 进行标记。
解决方案:从 Kindle 粘贴文本后,使用源代码编辑器中的正则表达式搜索功能执行搜索和替换。搜索正则表达式 \xA0 并将其替换为 \x20(或者,根据您的编辑器,只需在“替换”字段中输入单个空格字符 [这就是 Notepad++ 的工作原理])。
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]).
我会检查出版商的网站。我发现他们经常会发布书籍附带的源代码。如果他们不这样做,那么我购买这本书的可能性就会大大降低。 (在某些情况下,无论格式如何,无论进行多少复制/粘贴都无法检索到任何代码,因此尝试从发布者那里获取代码是唯一的方法)。
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).
我发现 PC 版 Kindle 只是将所有换行符转换为 \x20,使得查找和替换技巧不起作用。我的解决方案不是很好,但它有效:
我下载了 Calibre 并开始使用它。可以这么说,您可以通过谷歌搜索找到使用 Calibre 来打开您需要的任何内容的方法。
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.