Quellcode durchsuchen

Fixing cross linking to other chapters for WP

Swaroop C H vor 12 Jahren
Ursprung
Commit
e9fc324922
7 geänderte Dateien mit 253 neuen und 8 gelöschten Zeilen
  1. 2 2
      06-basics.pd
  2. 1 1
      08-control-flow.pd
  3. 2 2
      11-data-structures.pd
  4. 1 1
      14-io.pd
  5. 180 0
      22-translations.pd
  6. 20 0
      23-translation-howto.pd
  7. 47 2
      fabfile.py

+ 2 - 2
06-basics.pd

@@ -32,7 +32,7 @@ You can specify strings using single quotes such as `'Quote me on this'`. All wh
 
 Strings in double quotes work exactly the same way as strings in single quotes. An example is `"What's your name?"`
 
-### Triple Quote
+### Triple Quotes
 
 You can specify multi-line strings using triple quotes - (`"""` or `'''`). You can use single quotes and double quotes freely within the triple quotes. An example is:
 
@@ -257,7 +257,7 @@ is the same as
 print(i)
 ~~~
 
-Sometimes, there is an implicit assumption where you don't need to use a backslash. This is the case where the logical line uses parentheses, square brackets or curly braces. This is called **implicit line joining**.  You can see this in action when we write programs using [lists](#lists) in later chapters.
+Sometimes, there is an implicit assumption where you don't need to use a backslash. This is the case where the logical line uses parentheses, square brackets or curly braces. This is called **implicit line joining**.  You can see this in action when we write programs using [lists](#list) in later chapters.
 
 ### Indentation
 

+ 1 - 1
08-control-flow.pd

@@ -142,7 +142,7 @@ Note for C/C++ Programmers
 
 ## The for loop 
 
-The `for..in` statement is another looping statement which *iterates* over a sequence of objects i.e. go through each item in a sequence. We will see more about [sequences](#sequences) in detail in later chapters. What you need to know right now is that a sequence is just an ordered collection of items.
+The `for..in` statement is another looping statement which *iterates* over a sequence of objects i.e. go through each item in a sequence. We will see more about [sequences](#sequence) in detail in later chapters. What you need to know right now is that a sequence is just an ordered collection of items.
 
 Example:
 

+ 2 - 2
11-data-structures.pd

@@ -69,7 +69,7 @@ How It Works:
 
 The variable `shoplist` is a shopping list for someone who is going to the market. In `shoplist`, we only store strings of the names of the items to buy but you can add *any kind of object* to a list including numbers and even other lists.
 
-We have also used the `for..in` loop to iterate through the items of the list. By now, you must have realised that a list is also a sequence. The speciality of sequences will be discussed in a [later section](#sequences).
+We have also used the `for..in` loop to iterate through the items of the list. By now, you must have realised that a list is also a sequence. The speciality of sequences will be discussed in a [later section](#sequence).
 
 Notice the use of the `end` keyword argument to the `print` function to indicate that we want to end the output with a space instead of the usual line break.
 
@@ -214,7 +214,7 @@ Keyword Arguments and Dictionaries
 
 :   On a different note, if you have used keyword arguments in your functions, you have already used dictionaries! Just think about it - the key-value pair is specified by you in the parameter list of the function definition and when you access variables within your function, it is just a key access of a dictionary (which is called the *symbol table*in compiler design terminology).
 
-## Sequences 
+## Sequence 
 
 Lists, tuples and strings are examples of sequences, but what are sequences and what is so special about them?
 

+ 1 - 1
14-io.pd

@@ -43,7 +43,7 @@ Yes, it is a palindrome
 
 How It Works:
 
-We use the slicing feature to reverse the text. We've already seen how we can make [slices from sequences](#sequences) using the `seq[a:b]` code starting from position `a` to position `b`. We can also provide a third argument that determines the *step* by which the slicing is done. The default step is `1` because of which it returns a continuous part of the text. Giving a negative step, i.e., `-1` will return the text in reverse.
+We use the slicing feature to reverse the text. We've already seen how we can make [slices from sequences](#sequence) using the `seq[a:b]` code starting from position `a` to position `b`. We can also provide a third argument that determines the *step* by which the slicing is done. The default step is `1` because of which it returns a continuous part of the text. Giving a negative step, i.e., `-1` will return the text in reverse.
 
 The `input()` function takes a string as argument and displays it to the user. Then it waits for the user to type something and press the return key. Once the user has entered and pressed the return key, the `input()` function will then return that text the user has entered.
 

Datei-Diff unterdrückt, da er zu groß ist
+ 180 - 0
22-translations.pd


+ 20 - 0
23-translation-howto.pd

@@ -0,0 +1,20 @@
+# Translation Howto
+
+The full source of the book will be available from
+<https://github.com/swaroopch/byte_of_python>.
+
+You are encouraged to [fork the
+repository](https://help.github.com/articles/fork-a-repo).
+
+You need to know how to use [Git](http://www.git-scm.com) to be able
+to fetch the repository to your computer.
+
+Start editing the `.pd` files to your local language. Please go
+through the [Pandoc
+README](http://johnmacfarlane.net/pandoc/README.html#pandocs-markdown)
+to understand the formatting of the text.
+
+Then follow the instructions in the
+[README](http://github.com/swaroopch/byte_of_python#README) to install
+the software required to be able to convert the raw source files into
+PDFs, etc.

+ 47 - 2
fabfile.py

@@ -117,6 +117,16 @@ MARKDOWN_FILES = [
         'slug': "python_en-appendix_revision_history",
         'title': "Python : Appendix : Revision History",
     },
+    {
+        'file': '22-translations.pd',
+        'slug': "python_en-translations",
+        'title': "Python : Appendix : Translations",
+    },
+    {
+        'file': '23-translation-howto.pd',
+        'slug': "python_en-translation_howto",
+        'title': "Python : Appendix : Translation Howto",
+    },
 ]
 
 
@@ -303,6 +313,32 @@ http://docs.python.org/library/xmlrpclib.html
                               })
 
 
+def collect_header_anchors(chapter, i, all_headers):
+    soup = BeautifulSoup(chapter['html'])
+    for header in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']):
+        if 'id' in header.attrs:
+            all_headers[header['id']] = i
+
+
+def fix_links_to_other_chapters(chapter, chapters, all_headers):
+    """Fix links to other sections with Wordpress page URL."""
+    soup = BeautifulSoup(chapter['html'])
+    for link in soup.find_all('a'):
+        if 'href' in link.attrs:
+            if link['href'].startswith('#'):
+                header_id = link['href'][1:]
+                assert header_id in all_headers, \
+                    "#{} does not exist, referred in {}".format(
+                    header_id, chapter['file'])
+                other_chapter = chapters[all_headers[header_id]]
+                previous = link['href']
+                link['href'] = '{}#{}'.format(
+                    other_chapter['link'],
+                    header_id)
+                print("Replacing {} with {}".format(previous, link['href']))
+    chapter['html'] = unicode(soup)
+
+
 ##### Tasks ######################################
 
 
@@ -312,17 +348,26 @@ def wp():
     if WORDPRESS_ENABLED:
         chapters = copy.deepcopy(MARKDOWN_FILES)
 
+        # header anchor id -> index in MARKDOWN_FILES
+        all_headers = {}
+
         # Render html
         print("Rendering html")
-        for chapter in chapters:
+        for (i, chapter) in enumerate(chapters):
             chapter['html'] = markdown_to_html(open(chapter['file']).read(),
-                                               upload_assets_to_s3=True)
+                                               upload_assets_to_s3=AWS_ENABLED)
+
+            collect_header_anchors(chapter, i, all_headers)
 
             chapter['link'] = "{}/{}/{}".format(
                 os.environ['WORDPRESS_BASE_URL'],
                 os.environ['WORDPRESS_PARENT_PAGE_SLUG'],
                 chapter['slug'])
 
+        # Fix cross-links
+        for chapter in chapters:
+            fix_links_to_other_chapters(chapter, chapters, all_headers)
+
         # Add previous and next links at end of html
         for (i, chapter) in enumerate(chapters):
             previous_link = None