2015年9月3日 星期四

Blogger 上貼程式碼

說要做筆記,但是如果在 Blogger 上不能貼 code 的話,那可怎麼辦呢.

還好谷哥這次又幫了大忙了!不但讓我搜尋到了這個網頁,裡面有詳細的說明,甚至這個 solution 還是谷歌自己提供的呢.它的名字就叫做 code-prettify.

不囉唆,先來看看它的效果:
  1. override func didReceiveMemoryWarning() {
  2. super.didReceiveMemoryWarning()
  3. // Dispose of any resources that can be recreated.
  4. }
  5.  
  6. // MARK: - Table view data source
  7.  
  8. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  9. // Return the number of rows in the section.
  10. return self.restaurantNames.count
  11. }
  12.  
  13. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  14. let cellIdentifier = "Cell"
  15. let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell
  16. // Configure the cell...
  17. cell.nameLabel.text = restaurantNames[indexPath.row]
  18. cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row])
  19. cell.locationLabel.text = restaurantLocations[indexPath.row]
  20. cell.typeLabel.text = restaurantTypes[indexPath.row]
  21. cell.accessoryType = restaurantIsVisited[indexPath.row] ? .Checkmark : .None
  22.  
  23. // Circular image
  24. cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2
  25. cell.thumbnailImageView.clipsToBounds = true
  26. return cell
  27. }
還真的很漂亮呢.以下是 blogger 的修改方法:



1. 先將 “範本“ 和 ”版面配置“ 設定好,然後點選 ”編輯HTML



2. 然後在第一個 <\head> 和 <body> 中間插入這一行: (May 23/2016 更新,原先的連結已失效)
<script src='https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?lang=css&amp;skin=sons-of-obsidian'/>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=css&skin=sons-of-obsidian"/>


就像這樣:



3. 基本上就是貼上一行這麼簡單!存擋之後,只要將程式碼用下面這組 tag 包起來,就可以在 blogger 中呈現出來了:

<pre class="prettyprint">
your code 
</pre>


4. 如果還想用css加工一下也是可以的,以我的例子,我加了一個自動判別捲軸和最大圖形尺寸:



css碼如下:

  1. .post .prettyprint {
  2. overflow-x:auto;
  3. overflow-y:auto;
  4. max-height:400px;
  5. }

沒有這段的話其實也無所謂.如此一來,所有的程式碼都會同時顯示出來,也不會有捲軸.但不會影響閱讀.


沒有留言:

張貼留言