Update(2019.08.18): Add --unpublished option example to build ‘published: false’ post for local build.

Make jekyll blog post to private(not published)


Environment and Prerequisite

  • Jekyll
  • YAML


Front Matter’s published variable

When we make jekyll post, we always make yaml front matter.

Add published variable to front matter yaml and give value to private.

  • published: true: post to public
  • published: false: post to private

---
layout: post
title: [title post]
...
published: false
...
---

This option makes not to generate specific post. So if this option exist then its post will not be shown in local or server after build. If you’d like to see this post for test in local not in server, then add below option when you build.

  • Add --unpublished option when build
  • jekyll build command could be different depends on user’s system
bundler exec jekyll serve --unpublished
# OR
jekyll serve --unpublished


Reference

업데이트(2019.08.18): --unpublished를 이용해 빌드시 ‘published: false’로 되어있는 포스트도 빌드할 수 있는 방법 추가

블로그 글을 비공개로 해보자


환경 및 선수조건

  • Jekyll
  • YAML


머리말에 있는 published 변수

포스트를 만들 때마다 머리말에 있는 yaml을 설정해줘야 하는데 해당 yaml에서 published 변수를 설정하고 값을 설정해주면 된다.

  • published: true: 포스트 공개
  • published: false: 포스트 비공개

---
layout: post
title: [title post]
...
published: false
...
---

해당 옵션은 사이트가 생성될 때 특정한 포스트가 나타나지 않도록 하는 옵션으로 로컬이나 서버든 빌드를 하게 되면 볼 수 없게됩니다. 만약 로컬에서 테스트 용도로는 보고 싶고 서버에는 비공개로 해서 특정 포스트들을 빌드하고 싶지 않으시다면 아래 옵션을 이용해 로컬에서 빌드하시면 됩니다.

  • 빌드시 --unpublished 추가
  • jekyll을 빌드하는 방법은 사용자에 따라 다르니 참고하시기 바랍니다.
bundler exec jekyll serve --unpublished
# OR
jekyll serve --unpublished


참고자료

vglnk 속성 값 때문에 자동으로 태그가 씌워지는(wrap) 현상을 막아보자

환경 및 선수조건

  • Jekyll
  • YAML


class 속성에 nolinks를 추가

아래처럼 class="nolinks"를 추가해주면 된다.

...
<body class="nolinks">
...

만약 HTML 구조에서 특정 부분에만 적용을 하고 싶다면 아래처럼 특정태그에 적용을 해도 된다.

  • p 태그
...
<!--속성 추가-->
<p class="nolinks">
...
  • div 태그
...
<!--속성 추가-->
<div class="nolinks">

    ...contents...

</div>
...


업데이트(2018.05.16): requests 모듈을 통해서 웹에 있는 html 가져오는 부분 추가

웹 데이터 크롤링 또는 스크래핑을 할 때 사용하는 Python 라이브러리인 Beautiful Soup의 사용법에 대해서 간단하게 알아보자.


환경 및 선수조건

  • HTML, CSS, Javascript
  • Python
  • pip


Beautiful Soup 설치

$ pip install beautifulsoup4


Beautiful Soup 사용법

기본 세팅

기본적으로 패키지 import를 통해서 가져오며 html파일을 가져오거나 urllib 혹은 requests 모듈을 통해서 직접 웹에서 소스를 가져올 수도 있습니다.

package import

from bs4 import BeautifulSoup


html 파일 열기

with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')


urllib를 통해서 웹에 있는 소스 가져오기

  • web_url에 원하는 URL을 추가
import urllib.request
import urllib.parse

# web_url에 원하는 웹의 URL을 넣어주시면 됩니다.
with urllib.request.urlopen(web_url) as response:
    html = response.read()
    soup = BeautifulSoup(html, 'html.parser')


requests를 통해서 웹에 있는 소스 가져오기

  • web_url에 원하는 URL을 추가
import requests

# web_url에 원하는 웹의 URL을 넣어주시면 됩니다.
>>> r = requests.get(web_url)
>>> r.status_code
200
>>> r.headers['content-type']
'text/html; charset=UTF-8'
>>> r.encoding
'UTF-8'
>>> r.text
<!DOCTYPE html>
<html class="client-nojs" lang="en" dir="ltr">


HTML 예제

  • 아래 html 코드를 통해서 예제를 설명

example.html

<!DOCTYPE html>
<html>
	<head>
		<title>Page title</title>
	</head>
	<body>
    	<div>
            <p>a</p>
            <p>b</p>
            <p>c</p>
        </div>
        <div class="ex_class">
            <p>d</p>
            <p>e</p>
            <p>f</p>
        </div>
        <div id="ex_id">
            <p>g</p>
            <p>h</p>
            <p>i</p>
        </div>
		<h1>This is a heading</h1>
		<p>This is a paragraph.</p>
		<p>This is another paragraph.</p>
	</body>
</html>


find() 및 find_all()함수

  • 함수 인자로는 찾고자 하는 태그의 이름, 속성 기타 등등이 들어간다.
  • find_all(name, attrs, recursive, string, limit, **kwargs)
  • find(name, attrs, recursive, string, **kwargs)

find_all

  • find_all(): 해당 조건에 맞는 모든 태그들을 가져온다.
with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    all_divs = soup.find_all("div")
    print(all_divs)
# 출력 결과
[<div>
<p>a</p>
<p>b</p>
<p>c</p>
</div>, <div class="ex_class">
<p>d</p>
<p>e</p>
<p>f</p>
</div>, <div id="ex_id">
<p>g</p>
<p>h</p>
<p>i</p>
</div>]

find

  • find(): 해당 조건에 맞는 하나의 태그를 가져온다. 중복이면 가장 첫 번째 태그를 가져온다.
with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    first_div = soup.find("div")
    print(first_div)
# 출력 결과
<div>
<p>a</p>
<p>b</p>
<p>c</p>
</div>


태그를 이용해서 가져오기

  • 예제: 모든 <p> 태그들을 가져오기
with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    all_ps = soup.find_all("p")
    print(all_ps)
# 출력 결과
[<p>a</p>, <p>b</p>, <p>c</p>, <p>d</p>, <p>e</p>, <p>f</p>, <p>g</p>, <p>h</p>, <p>i</p>, <p>This is a paragraph.</p>, <p>This is another paragraph.</p>]
  • 예제: 첫번째 <div>태그를 가져오기
with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    first_div = soup.find("div")
    print(first_div)
# 출력 결과
<div>
<p>a</p>
<p>b</p>
<p>c</p>
</div>


태그와 속성을 이용해서 가져오기

태그와 속성을 이용할 때 함수의 인자로 원하는 태그를 첫번째 인자로 그 다음에 속성:값의 형태로 dictionary 형태로 만들어서 넣어주면 된다.

  • find_all('태그명', {'속성명': '값' ...})
  • find('태그명', {'속성명': '값' ...})

  • 예제: <div> 태그에서 id속성의 값이 ex_id인거 불러오기
with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    ex_id_divs = soup.find('div', {'id': 'ex_id'})
    print(ex_id_divs)
# 출력 결과
<div id="ex_id">
<p>g</p>
<p>h</p>
<p>i</p>
</div>


HTML 구조를 이용해 부분부분 가져오기

  • 예제: id속성의 값이 ex_id<div> 태그에서 <p>태그들만 가져오기
with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
    # id=ex_id인 div 태그를 가져와서
    ex_id_divs = soup.find("div", {"id":"ex_id"})
    # 그 태그들 안에서 p 태그를 가져온다.
    all_ps_in_ex_id_divs = ex_id_divs.find_all("p")
    print(all_ps_in_ex_id_divs)
# 출력 결과
[<p>g</p>, <p>h</p>, <p>i</p>]


쉘 스크립트(Shell Script)에서 pyenv와 virtualenv를 실행하는 방법을 알아보자.


환경 및 선수조건


쉘 스크립트에서 pyenv를 사용할 때

쉘 스크립트에서 아래처럼 pyenv activate [virtualenv name]을 사용하면(저의 경우에는 unopenlab이 가상환경 이름입니다.) 오류가 뜨게 되는 경우가 있습니다.

 #!/bin/bash
pyenv activate unopenlab

Error


쉘 스크립트에서 pyenv를 사용할 때 해결 방법

이 경우에 source ~/.bash_profile를 스크립트에 포함해서 사용해주면 됩니다. 환경 변수 및 다른것들을 새로 로드하기 때문에 위에 문제가 사라지게 됩니다.

  • Shell Script에 source ~/.bash_profile 한 줄 추가
#!/bin/bash
source ~/.bash_profile
pyenv activate unopenlab